var Versions = {}; 
Versions.Data = new Array (); 

Versions.GetVersion = function (versionID)
{
	var index = -1; 
	for (var i=0;i<Versions.Data.length;i++)
	{
		var Version = Versions.Data[i];
		if (Version.versionid == versionID)
		index = i; 
	}
	
	if (index < 0)
	{
		index = Versions.Data.length;
		Versions.Data[index]	= { versionid : versionID, images:new Array() };
	}
	
	return index;
}
Versions.AddImage = function (versionID,imageID,thumbName,Name,scaleWidth,scaleHeight)
{
	var index = Versions.GetVersion (versionID);

	if (index >= 0) 
	{
		Versions.Data[index].images[Versions.Data[index].images.length]	= 
		{
			imageid : imageID,
			thumbName : thumbName,
			name : Name,
			scalewidth : scaleWidth,
			scaleheight : scaleHeight
		}
		;
	}
}
Versions.MakeList = function (versionID)
{
	var Output = Array (); 
	for (var i=0;i<Versions.Data.length;i++)
	{
		var Version = Versions.Data[i];
		if (Version.versionid == versionID)
		{

			for (var m=0;m<Version.images.length;m++)
			{

				Output[Output.length] = Version.images[m];
			}
		}
	}
	for (var i=0;i<Versions.Data.length;i++)
	{
		var Version = Versions.Data[i];
		if (Version.versionid != versionID)
		{
			
			for (var m=0;m<Version.images.length;m++)
			{

				Output[Output.length] = Version.images[m];
			}
		}
	}
	return Output; 
}
Versions.UpdateView = function (versionID)
{
	var List = Versions.MakeList (versionID);

	for (var i=0;i<List.length;i++) 
	{
		var Img = List[i];
		
		var src	='/shop_image.php/'+Img.imageid+'/mini';
		if (i < 1)
		{
			$('#mainimg_link').attr('href',Img.name);
			
			$('#mainimg').attr('src',Img.thumbName+'/'+versionID+'/');
		}

		$('#img'+i+'_link').attr('href',Img.name);
		
		$('#img'+i).attr('src',src);
	}
}
Versions.Html = function () {
	var html = '<div id="combobox_version" class="combobox">';
    html 	+= '<div class="drop">';
	html 	+= '<div class="top"></div>';
	html 	+= '<div class="items">';
	
	var initialValue	= null;
	var initialLabel	= null;
	
	for (var i=0;i<ProductVersions.length;i++) 
	{
		var ver	= ProductVersions[i];
		
		if (initialValue == null)
		{
			initialValue	= ver.value;
			initialLabel	= ver.name;
		}
		
		html 	+= '<div class="item"><span class="la">'+ver.name+'</span>';
		html 	+= '<span class="va">'+ver.value+'</span>';
		html 	+= '</div>';
	}
	
	html 	+= '</div><div class="bottom"></div></div><div class="l"></div>';
	html 	+= '<div class="c"><div class="la">'+initialLabel+'</div><div class="va"></div></div>';
	html 	+= '<div class="r"></div>';
	html 	+= '<input name="versionid" value="'+initialValue+'" type="hidden" />';
	html 	+= '</div>';
	return html;
}


var Comboboxes 		= {};
Comboboxes.IDS		= new Array (); 
Comboboxes.Clicked	= new Array (); 

Comboboxes.Find = function (id)
{
	var Result = -1;
	for (var i=0;i<Comboboxes.IDS.length;i++)
	{
		var ID	= Comboboxes.IDS[i];
		
		if (ID == id)
		{
			Result = i;
			
		}
	}
	return Result; 
}

Comboboxes.GetState = function (ID)
{
	return ($('#'+ID+' .drop').css ('display')=='block');
}
Comboboxes.SetState = function (ID,State)
{
	return $('#'+ID+' .drop').css ('display',State?'block':'none');
}
Comboboxes.GlobalClick = function ()
{
	for (var i=0;i<Comboboxes.IDS.length;i++)
	{
		var ID	= Comboboxes.IDS[i];
		if (Comboboxes.GetState (ID) && !Comboboxes.Clicked[i])
		{
			Comboboxes.SetState (ID,false);
		}
	}
}
Comboboxes.ItemClick = function (Combo,Item)
{
	var ID		= $(Combo).attr ('id');
	$(Combo).find('.drop').css('display','none');
	$(Combo).find ('.c .va').html ($(Item).find('.va').html());
	$(Combo).find ('.c .la').html ($(Item).find('.la').html());
	$(Combo).find ('input').val ($(Item).find('.va').html());
}
Comboboxes.ItemHover = function (Combo,Item)
{
	var versionid	= $(Item).find ('.va').html();
	Versions.UpdateView (versionid);
	
}
Comboboxes.Release = function (el)
{
	var Index	= Comboboxes.Find ($(el).attr ('id'));
	if (Index >= 0)
	{
		Comboboxes.Clicked [Index]	 = false; 
	}
}
Comboboxes.Click = function (el)
{
	var ID		= $(el).attr ('id');
	var Index	= Comboboxes.Find (ID);
	if (Index >= 0)
	{
		Comboboxes.Clicked [Index]	 = true; 
	}
	$(el).find('.drop').css('display','block');
}

Comboboxes.Load		= function ()
{
	$ ('.combobox').each (function (index,element){
		var i = Comboboxes.IDS.length;
		Comboboxes.IDS [i] 		= $(element).attr ('id'); 
		Comboboxes.Clicked [i]	= 0;
		$('.combobox').mousedown (function () {
			Comboboxes.Click (this);
		});
		$('.combobox').mouseup (function () {
			Comboboxes.Release (this);
		});
		$('.combobox .drop .items .item').mouseup (function (){
			var Combo	= $(this).parent().parent().parent();
			Comboboxes.ItemClick (Combo,this);
		});
		$('.combobox .drop .items .item').mouseover (function (){
			var Combo	= $(this).parent().parent().parent();
			Comboboxes.ItemHover (Combo,this);
		});
	});
	
	
}



Search = {};

$(document).ready(function() {
	
	if ($('#productversion').length > 0)
	$('#productversion').html (Versions.Html());
	
	$('#afleveradres').bind ('change',function (){
		
		$('#afleveradresBlock').css('display',$('#afleveradres').attr("checked")?'block':'none');
	}); 
	
	$('html').click (function() { if (!Search.IgnoreClick) { Search.Hide (); }});
	$('#search input').click 	(function () { Search.Query=''; Search.Run (); });
	$('#search input').keyup 	(function (e) { if (e.keyCode != 38 && e.keyCode!=40) { Search.Run (e); } });
	$('#searchbox').mouseover 	(function () { Search.IgnoreClick = true; });
	$('#searchbox').mouseout 	(function () { Search.IgnoreClick = false; });
	$(document).keydown			(function (e){ return Search.KeyDown (e); });
	$(document).keyup		(function (e){ return Search.FuncUp (e); });
	
	Search.Running		= false; 
	Search.Query		= '';
	Search.Shown		= false; 
	Search.LastChecked	= 0;
	Search.NextHideTime	= 0; 
	Search.IgnoreClick	= false; 
	Search.Selected		= -1;
	Search.ShiftDown	= false;
	Search.ControlDown	= false; 
	Search.SetFocus		= false;
	setTimeout ('Search.Tick();',1000); 	
});

Search.FuncUp = function (e)
{
	if (e.keyCode == 16)
	{
		this.ShiftDown	= false;
		return false;
	}
	if (e.keyCode == 70)
	{
		if (this.SetFocus)
		{
			this.SetFocus = false;
			//$('#search input').focus ();
		}
	}
	this.ControlDown	= false; 
	
	if (e.keyCode!=70 && e.keyCode!=16)
	{
		Search.SetFocus = false; 
	}
}
Search.FuncDown = function (e)
{
	if (e.keyCode == 16)
	{
		Search.ShiftDown	= true;
	}
	else if (e.keyCode == 17)
	{
		Search.ControlDown	= true;
	}
	else if (e.keyCode == 70)
	{
		if (this.ShiftDown)
		{
			Search.SetFocus = true; 
		}
	}
	
	if (e.keyCode!=70 && e.keyCode!=16)
	{
		Search.SetFocus = false; 
	}
}

Search.KeyDown = function (e)
{
	//Search.Show ();
	if (e.keyCode == 38)
	{
		if (Search.Selected > 0)
		Search.Selected --;
		else
		Search.Selected = 4;
		Search.SelectRow(Search.Selected);
	}
	else if (e.keyCode == 40)
	{
		if (Search.Selected < 4)
		{
			Search.Selected += 1; 
		}
		else
		Search.Selected = 0;
		Search.SelectRow(Search.Selected);
	}
	else if (e.keyCode == 13)
	{
		Search.ClickRow(Search.Selected);
	}
	else
	Search.FuncDown(e);
}

function ClickLink (el)
{
	var tdElements	= el.getElementsByTagName ('td');
	
	if (tdElements.length > 0)
	{
		var td			= tdElements[0];
		var aElements 	= td.getElementsByTagName ('a');

		if (aElements.length > 0)
		{
			var a 			= aElements[0];
			window.location = a.href;
		}
	}
}

Search.ClickRow = function (Row)
{
	
	this.Selected	= Row; 
	var Children	= $('#results .tbproducts tbody').children ();
	
	for (var i=0;i<Children.length;i++)
	{
		var Child = Children[i];
		if (i == Row)
		$(Child).click ();
	}
}

Search.SelectRow = function (Row)
{
	this.Selected	= Row; 
	var Children	= $('#results .tbproducts tbody').children ();
	for (var i=0;i<Children.length;i++)
	{
		var Child = Children[i];
		$(Child).attr("id",(i == Row)?'searchkey':'');
	}
}

Search.RowHover = function (ID,El,Leave)
{
	if (!Leave)
	{
		this.SelectRow (ID);
	}
	else
	{
		this.SelectRow (-1);
	}
}
Search.Tick = function ()
{
	var Time = Search.GetTime ();
	if (Time >= Search.NextHideTime)
	{
		if (Search.Shown)
		Search.Hide ();
	}
	if (Search.Query != $('#search input').val ()) 
	{
		Search.Run ();
	}
	
	setTimeout ('Search.Tick();',1000); 
}
Search.GetTime = function ()
{
	var d = new Date();
	return d.getTime();
}
Search.Show = function ()
{
	$('.searchbox').css ('display','inline');
	
	Search.Shown			= true; 
	Search.NextHideTime	= this.GetTime ()+10000; 	
}
Search.Hide = function ()
{
	$('.searchbox').css ('display','none');
	Search.Shown	= false; 
}

Search.Run = function ()
{
		Search.Selected		= 0;
		
		
		if ($('#search input').val ().length < 1)
		return false; 
		
		if ($('#search input').val () == Search.Query)
		return false; 
		
		if (this.Running) return false; // mag niet
		this.Show (); 
		
		Search.Query		= $('#search input').val ();  
		
		Search.Running		= true;
		
		Search.Show ();
		$('#search .loading').css ('display','inline-block');
		$.ajax({type:'POST',url:'/search',data: {'query':Search.Query},
		success: function (data) {
			Search.Show ();
			var items 	= $.parseJSON(data);
			var output	= '<table width="100%" class="tbproducts" cellpadding="1" cellspacing="1" border="0"><tbody>';
			for (var i=0;i<items.length;i++)
			{
				var itm	= items[i];
				output += '<tr onmouseover="Search.RowHover('+i+',this,false);" onmouseout="Search.RowHover ('+i+',this,true);" onclick="ClickLink (this);" class="r1"><td width="20"><a href="/shop_product/'+itm['p']+'"><img width="40" src="/shop_image.php/'+itm['i']+'/mini/" border="0"></a></td><td><a href="/shop_product/'+itm['p']+'">'+itm['n']+'</a></td></tr>';
			}
			output += '</tbody></table>';
			$('#search #results').html (output);
			$('#search .loading').css ('display','none');
			Search.SelectRow(Search.Selected);
			if (Search.Query != $('#search input').val ()) 
			{
				Search.Run ();
			}
		}, 
		
		complete: function () {
			//$('#search .loading').css ('display','none');
			Search.Running = false; 
			//Search.Hide (); 
			
		}});
	
}


Validator = {};

Validator.Length	= 0x01;
Validator.Email		= 0x02;
Validator.Match		= 0x04;
Validator.Discount	= 0x08;
Validator.CheckingDiscount	= false;
Validator.LastCheck			= 0;
Validator.TimeoutDiscount	= false;

Validator.GetTime = function ()
{
	var d = new Date();
	return d.getTime();
}

Validator.Validate = function (El,Flags,Width,Userdata)
{
	var Valid	= true;
	
	if (Flags & this.Length)
	{
		if ($(El).val().length < 1)
		{
			Valid = false;
		}
	}
	if (Valid)
	{
		if (Flags & this.Email)
		{
			var reg = /^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$/;
			//var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if (!reg.test($(El).val()))
			{
				Valid = false; 
			}
		}
	}
	if (Valid)
	{
		if (Flags & this.Match)
		{
			var otherVal	= $('#'+Userdata).val();
			if ($(El).val()!=otherVal)
			Valid = false; 
		}
	}
	if (Valid)
	{
		if (Flags & this.Discount)
		{
			//Validator.CheckDiscount ();
			return false;
		}
	}
	
	$(El).css ('background-image','url(/input.php?w='+Width+'&color='+(Valid?'green':'red')+')');
} 
Validator.CheckDiscount = function ()
{
	var Time	= this.GetTime ();
	Validator.TimeoutDiscount	= false; 
	if (!Validator.CheckingDiscount)
	{
		if ((Time-Validator.LastCheck)>100)
		{
			Validator.LastCheck			= Time;
			Validator.CheckingDiscount = true;
			$.ajax({type:'POST',url:'/controlpanel/shop/ajax_discount',data: {'code':$('#Discount').val()},
			success: function (data) 
			{
				$('#Discount').css ('background-image','url(/input.php?w='+300+'&color='+((data=='1')?'green':'red')+')');
				Validator.CheckingDiscount	= false;
			}});
		}
		else
		{
			if (!Validator.TimeoutDiscount)
			{
				setTimeout ('Validator.CheckDiscount()',1000);
				Validator.TimeoutDiscount = true;
			}
		}
	}
	else
	{
		if (!Validator.TimeoutDiscount)
		{
			setTimeout ('Validator.CheckDiscount()',1000);
			Validator.TimeoutDiscount = true;
		}
	}
}
Validator.Restore = function (El,Width)
{
	//$(El).css ('background-image','url(/input.php?w='+Width+'&color=none)');
}
Validator.PragmaticChange	= function (El)
{
	$('.pragmatic').css ('display',$('#pragmaticYes').attr("checked")?'table-row':'none');
}


