﻿domain = 'http://omat/';
domain = 'http://www.azzurromodels.fi/';
//domain = 'http://localhost/azzurro/';

//voice teaser


Voiceteaser = function ()
{

	var hidden = new SWFObject("mp3player/ep_player.swf", "hidden_player", "269", "458", "7", "#ffffff");
	hidden.addVariable("skin", "mp3player/skins/azzurro/skin.xml");
	hidden.addVariable("autoplay", "false");
	hidden.addVariable("shuffle", "false");
	hidden.addVariable("repeat", "false");
	hidden.addVariable("buffertime", "1");
	hidden.addParam("allowscriptaccess", "always");
	hidden.addVariable("notifyonevent", "true");
	hidden.addVariable("key", "AEE1GMP5DS7SFO4D326A");
	hidden.addVariable("file", "<location>/voices/demo.mp3</location><creator>xx</creator><title>xx</title>");
	hidden.write("mp3player_");
	var this_ = this;
	this.playing = false;
	$('.voice-teaser img').click(function(){
		if(this_.playing)
		{
			EP_pause("hidden_player");
			this_.playing = false;
		}
		else
		{
			EP_play("hidden_player");
			this_.playing = true;	
		}
	});
	
	EP_eventHandler = function(e)
	{
		if(e == 'MP3_COMPLETE')
			$('.voice-teaser a').show();
	}
};

Voice = function ()
{
	var this_ = this;
	this.init = function(data)
	{
		$.get(domain + "voiceses.php", {}, function(transport)
		{
			var so = new SWFObject("mp3player/ep_player.swf", "azzurro_player", "269", "458", "7", "#ffffff");
			so.addVariable("skin", "mp3player/skins/azzurro/skin.xml");
			//so.addVariable("playlist", "mp3player/playlist.xml");
			so.addVariable("autoplay", "false");
			so.addVariable("shuffle", "false");
			so.addVariable("repeat", "false");
			so.addVariable("buffertime", "1");
			so.addParam("allowscriptaccess", "always");
			so.addVariable("key", "AEE1GMP5DS7SFO4D326A");
			so.addVariable("playlistxml", transport);
			so.addVariable("notifyonevent", "true");
			so.write("mp3player");
		});
		
		var hidden = new SWFObject("mp3player/ep_player.swf", "hidden_player", "269", "458", "7", "#ffffff");
		hidden.addVariable("skin", "mp3player/skins/azzurro/skin.xml");
		hidden.addVariable("autoplay", "false");
		hidden.addVariable("shuffle", "false");
		hidden.addVariable("repeat", "false");
		hidden.addVariable("buffertime", "1");
		hidden.addParam("allowscriptaccess", "always");
		hidden.addVariable("key", "AEE1GMP5DS7SFO4D326A");
		hidden.addVariable("notifyonevent", "true");
		hidden.write("mp3player_");
		
		this.form =  $("#voicehaku");
		this.tulos = $('#voicetulos');
		this.form.ajaxForm({success: this_.searchresult});
		$('input.list').livequery('click', this.list);
		
		$('input[value=play]').livequery('click', this.play);
		$('input[value=download]').livequery('click', this.download);
		$('input.add').livequery('click', this.add);
		$('input.remove').livequery('click', this.remove);

		$('input.downloadall').livequery('click', this.downloadAll);
		$('input.removeall').livequery('click', this.removeAll);
		
		EP_eventHandler = this.evt;
	};
	
	this.list = function()
	{
		$.get(domain + "voicehakutulos.php", {ses: 1}, function(r)
		{
			this_.tulos.html(r);
		});
	};
	
	this.add = function()
	{
		var p = $(this).parent().parent().parent().parent().parent();
		var id = $('.id', p).text();
		
		$.get(domain + "voiceses.php", {add: id}, function(transport)
		{
			EP_setPlayListXML("azzurro_player", transport);
		});
	
	};
	
	this.remove = function()
	{
		var p = $(this).parent().parent().parent().parent().parent();
		var id = $('.id', p).text();
		
		$.get(domain + "voiceses.php", {remove: id}, function(transport)
		{
			EP_setPlayListXML("azzurro_player", transport);
			if(p.hasClass('deletable')) p.hide('slow');
		});
	
	};

	this.removeAll = function()
	{
		
		$.get(domain + "voiceses.php", {removeAll: 1}, function(transport)
		{
			EP_setPlayListXML("azzurro_player", transport);
			$('.deletable, center').hide('slow');
		});
	
	};

	this.playing = 0;
	this.play = function()
	{
		if($(this).val() == 'stop')
		{
			$(this).val('play');
			EP_stop("hidden_player");
			return;
		}
		$('input[value=stop]').val('play');
		$(this).val('stop');
		var p = $(this).parent().parent().parent().parent().parent();
		var file = $('.file', p).text();
		var id = $('.id', p).text();
		
		EP_loadMP3("hidden_player", "<location>/voices/" + file + "</location><creator>Song Creator</creator><title>Song Title</title>");
		EP_pause("azzurro_player");
		EP_play("hidden_player");
	};
	
	this.download = function()
	{
		var p = $(this).parent().parent().parent().parent().parent();
		var file = $('.file', p).text();
		
		document.location = '/voices/' + file;
	};

	
	this.downloadAll = function()
	{
		
		document.location = '/voices/downloadvoices.php';
	};
	
	
	this.evt = function(e)
	{
		switch(e)
		{
			case 'MP3_COMPLETE':
				$('input[value=stop]').val('play');
			break;
			
			case 'MP3_SELECT':		
			case 'MP3_PLAYING':
				this_.playing ++;
				if(this_.playing > 2)
				{
					$('input[value=stop]').val('play');
					EP_stop("hidden_player");
				}
			break;
			
			case 'MP3_STOPPED':
				this_.playing = 0;
			break;
		}
	}
	
	this.searchresult = function(r)
	{
		this_.tulos.html(r);
	}
	
	this.init();
}



ModelSelection = function()
{
	var this_ = this;
	this.allModels = [];
	this.init = function(data)
	{
		this.strip =  $("#model_strip");
		this.smalls4 = [$("#small4_0"), $("#small4_1") ,$("#small4_2"), $("#small4_3")];
		$(".small4").livequery('click', this.tinyOnclick);
		$('.clearStrip').livequery('click', this.clearStrip);
		this.savedModels = this.models = data.models;
		this.currentModel = data.currentModel;
		
		this.renderTinys();
		this.renderStrip(0, 1);
		if(typeof(pagination) != 'undefined') pagination.moveTo(0);
	
	};
	
	this.tinyOnclick = function(elm, parts)
	{

		var tgt = elm.currentTarget || elm.target.parentElement;
		var parts = tgt.className.substr(5,100).split(' ');
		if(parts[2] && parts[2] == 'strip')
		{
			var f = document.forms['muisti'];
			if(!f)
				document.location  = domain + 'mallitiedot.php?model_id=' + parts[1];
			var mem = f['models_post'].value;
			if(mem != '')
			{
				var m = mem.split(',');
				for(var l = 0; l < m.length; l++)
				{
					if(m[l] == parts[1])
					{
						
						
						f['index'].value = l;
						f['model_id'].value = parts[1];
						f.submit();
						return;
					}
				}
				
				f['models_post'].value = m.splice(f['index'].value,0,parts[1]).join(',');
				f['index'].value = Number(f['index'].value + 1);
				f['model_id'].value = parts[1];
				f.submit();
				return;
			}
			document.location  = domain + 'mallitiedot.php?model_id=' + parts[1];
			return;
		}
		
		var pic_no = parts[1];
		if(this_.currentModel.imgs[pic_no])
		{
			this_.currentModel.imgs.splice(pic_no,1);
			this_.renderTinys(pic_no);
			if(this_.currentModel.imgs.length == 0)
				this_.clearSelection(this_.currentModel.model_id);
				
			this_.renderStrip(pic_no);
			
		}
	};

	this.clearSelection = function(model_id)
	{
		delete this.models[model_id];
		this.renderStrip(0);
	};
	
	this.clearStrip = function()
	{
		this_.models = {};
		this_.currentModel.imgs =[];
		this_.renderTinys();
		this_.renderStrip(0);
		$('#selected_models_list').html('Nothing selected.');
	};
	
	this.addTo4 = function(model_id, pic_no)
	{
		var c = 0;
		for(var i in this_.models) c++;
		if(c > 19) return;
		var ml = this.currentModel.imgs.length;
		if(ml == 4) return;
		for(var l = 0; l < ml; l++)
		{
			if(this.currentModel.imgs[l] == pic_no) return;
		}
		this.currentModel.imgs.push(pic_no);
		this.renderTinys(ml);
		this.models[this.currentModel.model_id] = {model_id: this.currentModel.model_id, imgs: this.currentModel.imgs};
		
		this.renderStrip(ml);
	
	};
	
				
	this.renderTinys = function(from)
	{
		var from  = from | 0;
		for(var l = from; l < 4; l++)
		{
			var html = !this_.currentModel.imgs[l] ? '' : '<img width="100%" src="http://www.azzurromodels.fi/modpics/' + this_.currentModel.model_id + '_' + this_.currentModel.imgs[l] + '_small.jpg" />';
			this_.smalls4[l].html(html);
		}
		if(this_.currentModel.imgs.length > 0)
		{
			$('.yes').addClass('selected');
			$('.no').removeClass('selected');
		}
		else
		{
			$('.no').addClass('selected');
			$('.yes').removeClass('selected');
		}
	};
	
	this.renderStrip = function(index, noSave)
	{
	
		if(noSave != 1)
		{
			var json  = !isEmpty(this.models)? {'models': $.toJSON(this.models)} : {models: ''};
/*
			$.ajax({url :domain + "modses.php",
			cache: false,
			dataType: 'json', 
			data : json, 
			success: function(transport)
			{
				alert(index + ' ' + noSave);
				alert(transport);
			});
			*/
			$.getJSON(domain + "modses.php", json, function(transport)
			{
				//alert(transport);
			});

		}
		
		if(index == 0)
		{
			var html = '';
			
			for(var model_id in this.models)
			{
				html += '<img class="small4 ' + model_id + ' strip" src="http://www.azzurromodels.fi/modpics/' + model_id + '_' + this.models[model_id].imgs[0] + '_small.jpg" />';
			}
			
			html  = '<div class="strip_imgs">' + html + '</div>';
			var selection = 'selection <a href="valitut.php">&nbsp;&nbsp;&nbsp;&nbsp; view</a>|<a target="_blank" href="pdf.php">pdf</a>|<a class="clearStrip">clear</a><div style="clear: both;"></div>';
			this.strip.html(selection + html);
		}
	};



};



function xpic(mi,no)
{
	modelselection.addTo4(mi, no);
	return;
}

function PicPagination(pages)
{
	this.index = 0;
	this.pages = pages - 2;
	
	this.moveTo = function(no)
	{
		$('#linkit div').css({'text-decoration': 'none'});
		$('#linkit div.' + (no + 1)).css({'text-decoration': 'underline'});
		$('#big_imgs td').hide();
		$('.big_' + no).show();
		if(no > 0) $('#linkit .previous').css('visibility', 'visible');
		else $('#linkit .previous').css('visibility', 'hidden');
		if(no <= this.pages) $('#linkit .next').css('visibility', 'visible');
		else $('#linkit .next').css('visibility', 'hidden');
		
		this.index = no;
	};
	
	this.next = function()
	{
		this.moveTo(this.index + 1);
	};
	
	this.previous = function()
	{
		this.moveTo(this.index - 1);
	};
}

function YesNoSelection(selection, model_id)
{
	if(selection) $('.yes').addClass('selected');
	else $('.no').addClass('selected');
	
	$('.yes').bind('click', function()
	{
		if(modelselection.currentModel.imgs.length > 0) return;
		modelselection.addTo4(modelselection.currentModel.model_id, $('.big_0 img')[0].name.replace('img[', '').replace(']', ''));
	});
	$('.no, .delete').bind('click', function()
	{
		modelselection.clearSelection(modelselection.currentModel.model_id);
		modelselection.currentModel.imgs = []; modelselection.renderTinys(0);
	});
	
	
}
function SelectionPagination()
{
	var this_ = this;
	this.index = 0;
	this.perPage = 4;
	this.items = $('#selected_models_list .selected_list_item');
	this.pages = Math.ceil(this.items.length / this.perPage);
	
	$('.delete').bind('click', function()
	{
		
		var model_id = this.className.split(' ')[1];
		$(this.parentNode.parentNode).remove();
		
		modelselection.clearSelection(model_id);
		this_.items = $('#selected_models_list .selected_list_item');
		this_.pages = Math.ceil(this_.items.length / this_.perPage);
		var linkit = $('#linkit div');
		for(var l = 0; l < linkit.length; l++)
		{
			if(Number(linkit[l].className.split(' ')[1]) > this_.pages)
			{
				$(linkit[l]).remove();
				if(this_.index == l -1)
				{
					if(l > 1) this_.moveTo(l - 2);
					else $('#selected_models_list').html('Nothing selected.');
				}
			}
		}
		this_.moveTo(this_.index);
	});
	
	
	this.moveTo = function(no)
	{
		$('#linkit div').css({'text-decoration': 'none'});
		$('#linkit div.' + (no + 1)).css({'text-decoration': 'underline'});
		
		this.items.hide();
		for(var l = no * this.perPage; l < (no + 1) * this.perPage; l++)
		{
			if(this.items[l])
			{
				$(this.items[l]).show();
			}
		}
		if(no > 0) $('#linkit .previous').css('visibility', 'visible');
		else $('#linkit .previous').css('visibility', 'hidden');
		if(no < this.pages -1) $('#linkit .next').css('visibility', 'visible');
		else $('#linkit .next').css('visibility', 'hidden');
		
		this.index = no;
	};
	
	this.next = function()
	{
		this.moveTo(this.index + 1);
	};
	
	this.previous = function()
	{
		this.moveTo(this.index - 1);
	};
	
	this.moveTo(0);
}
	
	
function isEmpty(o) {
    var i, v;
    if (typeof(o) === 'object') {
        for (i in o) {
            v = o[i];
            if (v !== undefined && typeof(v) !== 'function') {
                return false;
            }
        }
    }
    return true;
}

if(document.all)
{
var console = {
	debug: function(){}
};
}

SearchResultModelInfo = function()
{
	$('.modpic').hover(
		function(elm)
		{
			var tgt = elm.currentTarget || elm.target;
			var pos = tgt.className.split(' ')[1].substr(7,100);
			$('.hideme_' + pos).hide();
			$('.modinfo_' + pos).show();
		},
		
		function()
		{
			$('.modpic').show();
			$('.modinfo').hide();
		}
	);
}



MainMenu = function(startPage, startSub)
{
	var this_ = this;
	this.startPage = startPage;
	this.startSub = startSub;

	
	$('#top_menu_level_1 a').hover(
		function()
		{
			this_.stopRevert();
			var name = this.className.split(' ')[0];
			$('#top_menu_level_1 a').removeClass('active');
			$('#top_menu_level_1 a.' + name).addClass('active');
			$('#top_menu_level_2 div').hide();
			$('#top_menu_level_2 div.' + name).show();
			//console.debug(this);
			
		},
		
		function()
		{
			this_.startRevert();
		}
	);
	
	$('#top_menu_level_2').hover(
		function()
		{
			this_.stopRevert();
		},
		
		function()
		{
			this_.startRevert();
		}
	);
	
	this.stopRevert = function()
	{
		clearTimeout(this.clearing);
	};
	
	this.startRevert = function()
	{
		this.clearing = setTimeout(this.revert, 1000);
	};
	
	this.revert = function()
	{
		$('#top_menu_level_1 a').removeClass('active');
		$('#top_menu_level_1 a.' + this_.startPage).addClass('active');
		$('#top_menu_level_2 div').hide();
		$('#top_menu_level_2 div.' + this_.startPage).show();
	};
	if(this_.startSub >= 0)
	{
		var sub = $('#top_menu_level_2 div.' + this_.startPage + ' a')[this_.startSub];
		//console.debug(typeof(sub));
		if(sub != undefined) sub.className += ' active';
	}
	this.revert();
}


function laheta_hakemus()
{
	var err="";
	var lang  = 
	{
		fi: 
		{
			nimi: 'nimi',
			ika: 'ik�',
			vaatekoko : 'vaatekoko',
			pituus: 'pituus',
			puhelin: 'puhelin',
			kaupunki: 'kaupunki',
			kuva: 'kuva',
			postinumero: 'postinumero- ja toimipaikka',
			puuttuu: 'puuttuu'
		},
			
		en:
		{
			nimi: 'name',
			ika: 'age',
			vaatekoko : 'size',
			pituus: 'height',
			puhelin: 'phone',
			kaupunki: 'city',
			kuva: 'picture',
			postinumero: 'zip code and city',
			puuttuu: 'missing'
		}
	}
	d=document.forms.formi;
	var l = d.lang.value;
	if(d.nimi.value=="") err+= lang[l]['nimi'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.ika.value=="") err+= lang[l]['ika'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.vaatekoko.value=="") err+= lang[l]['vaatekoko'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.pituus.value=="") err+=lang[l]['pituus'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.puhelin.value=="") err+= lang[l]['puhelin'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.postinumero.value=="") err+= lang[l]['postinumero'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.kuva.value=="") err+= lang[l]['kuva'] + ' ' +lang[l]['puuttuu'] + "\n";
	
	if(err=="")
	{
		d.submit();
	}	
	else
	{
		alert (err);
	}

}


function laheta_info()
{
	var err="";
	var lang  = 
	{
		fi: 
		{
			nimi: 'nimi',
			yritys: 'yritys',
			puhelin: 'puhelin',
			puuttuu: 'puuttuu'
		},
			
		en:
		{
			nimi: 'name',
			yritys: 'company',
			puhelin: 'phone',
			puuttuu: 'missing'
		}
	}
	d=document.forms.formi;
	var l = d.lang.value;
	if(d.nimi.value=="") err+= lang[l]['nimi'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.yritys.value=="") err+= lang[l]['yritys'] + ' ' + lang[l]['puuttuu'] + "\n";
	if(d.puhelin.value=="") err+= lang[l]['puhelin'] + ' ' + lang[l]['puuttuu'] + "\n";
	
	
	if(err=="")
	{
		d.submit();
	}	
	else
	{
		alert (err);
	}

}


function laheta_kilpailu()
{	
	d=document.forms.formi;
	var err="";

	if(d.etunimi.value=="") err+="etunimi puuttuu\n";
	if(d.sukunimi.value=="") err+="sukunimi puuttuu\n";
	if(d.os.value=="") err+="postiosoite puuttuu\n";
	if(d.pno.value=="") err+="postinumero puuttuu\n";
	if(d.ptp.value=="") err+="postitoimipaikka puuttuu\n";
	if(d.puh.value=="") err+="puhelinnumero puuttuu\n";
	if(d.email.value=="") err+="s�hk�postiosoite puuttuu\n";
	if(d.ika.value=="") err+="ik� puuttuu\n";
	if(d.sukupuoli.value=="") err+="sukupuoli puuttuu\n";


	if(err=="")
	{
		d.submit();
	}	
	else
	{
		alert (err);
	}

}


function newwin(id, index)
{
	var models_post = document.forms['kuvat'].models_post.value;
	document.location = 'mallitiedot.php?model_id=' + id + '&index=' + index + '&models_post=' + models_post;
}
