//enable firebug
var console;

//make uniqID
var vuid=0;
var uid = function(){
	return vuid++ ;
}

//dummy
var popInfo = function(){void(0);}

//emulate popup function...
var popWindow = function(u,w,h){
	e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
	if(e == 'png' || e == 'gif' || e == 'jpg')
		popEl = $("<div class=\"popWindowDiv clear\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
	else
		popEl = $("<div class=\"popWindowDiv clear\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	popEl.modal({
		onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						dialog.container.slideDown('slow', function () {
							dialog.data.fadeIn('slow'); // See Other Notes below regarding
													   // data display property and
													   // iframe details
						});
					});
				},
		onClose: function (dialog) {
					dialog.data.fadeOut('slow', function () {
					  dialog.container.slideUp('slow', function () {
						dialog.overlay.fadeOut('slow', function () {
						  $.modal.close(); // must call this to have SimpleModal
										   // re-insert the data correctly and
										   // clean up the dialog elements
						});
					  });
					});
				}
	});
	$("#modalContainer").css('height',h).css('width',w).css('margin-left',(w / 2) * -1); 
}


//start scripts
$(document).ready(function(){
	
	//main menu
	$("#menu ul li").mouseover(function(){
		if($(this).find('ul').is(':hidden')){
			$("#menu ul li ul.open").fadeIn('normal');
			$(this).find('ul:first').fadeIn('normal').addClass('open');
		}
	});
	
	
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = 0;

		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				u = $(this).attr('href');
				e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + img[id].src + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick').toString();
					h = 600;
					w = 800;
					if(c){
						h = c.substring(c.indexOf('height=') + 7);
						h = h.substring(0,h.indexOf(','));
		
						w = c.substring(c.indexOf('width=') + 6);
						w = w.substring(0,w.indexOf(','));
					}
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + $(this).attr('href') + "'," + w + "," + h + ");");				
				}
			}
		}
	});	
		
	//change shop scroller.
	$("#shop_scroller").append('<div class="popInfoBox"></div>');
	$("#shop_scroller div.items").append('<div class="left"></div>').append('<div class="right"></div>');
	shop_scroller_AddItemHover = function(){
		$("#shop_scroller div.items a.item").hover(
			function(){
				id = $(this).attr('id');
				o = $(this).offset();
				pib = $("#shop_scroller .popInfoBox").clone();
				$("#shop_scroller .items").append(pib);
				pib.attr('id','pib_' + id).css('left',o.left).html($('div.' + id).html());
				pib.fadeIn('slow');
				
				if($(this).hasClass('faded'))
					$(this).fadeTo('slow',1);
			},
			function(){
				$('#shop_scroller .items .popInfoBox').remove();
				if($(this).hasClass('faded'))
					$(this).fadeTo('slow',0.3);
			}
		);
		$("#shop_scroller .item_info").wrapInner('<div class="block"></div>');
	}
	shop_scroller_AddItemHover();
	var scroll_view_amount = 6;
	$("#shop_scroller .items div.right").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
		$("#shop_scroller div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
			$("#shop_scroller div.scrollContent a.item:lt(" + scroll_view_amount + ")").appendTo("#shop_scroller div.scrollContent");
			$("#shop_scroller div.scrollContent").css('left','0px');
		});
	});
	$("#shop_scroller .items div.left").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
		i = $("#shop_scroller div.scrollContent a.item").length - (scroll_view_amount + 1);
		$("#shop_scroller div.scrollContent a.item:gt(" + i + ")").prependTo("#shop_scroller div.scrollContent");
		$("#shop_scroller div.scrollContent").css('left',(x * -1) + 'px');
		$("#shop_scroller div.scrollContent").animate({'left':'0px'}, "slow","swing",function(){
		});
	});
	$("#shop_scroller .categories a.category").click(function(e){
		e.preventDefault;
		$("#shop_scroller .categories a.current_categoryID").removeClass('current_categoryID');
		$(this).addClass('current_categoryID');
		i = $(this).attr('href').match(/categoryID=(\d+)/)[1];
		
		//scroll to first occurence.
		if($("#shop_scroller .scrollContent .item_cat_" + i + ":eq(0)").length){
			var x = $("#shop_scroller .scrollContent a.item_cat_" + i + ":eq(0)").position().left;
			var y = $("#shop_scroller div.scrollContent a:eq(0)").width() * (4 + Math.round($("#shop_scroller .scrollContent a.item_cat_" + i).length / 2));
			
			$("#shop_scroller .scrollContent a.item.faded").fadeTo('slow',1).removeClass('faded');
			$("#shop_scroller .scrollContent a.item").not("a.item_cat_" + i).fadeTo('slow',0.3).addClass('faded');
			
			$("#shop_scroller .scrollContent").animate({'left':(x * -1) +'px'}, "slow","swing",function(){
				$(this).css('left','0px').children("a.item").each(function(){
					if($(this).position().left < x)
						$(this).addClass('append');
				});
				$(this).children("a.item.append").appendTo("#shop_scroller div.scrollContent").removeClass('append');
			});
		}
		return false;
	});
	
	
	//rebuild store listing
	$(".store_module .listing div.article").addClass('article_container').addClass('clear');
	$(".store_module .listing .specs").addClass('clear');
	//$(".store_module .listing").each(function(){
	//	var maxHeightTitle = 0;
	//	$(this).find("div.article .title").each(function(){
	//		if($(this).height() > maxHeightTitle)
	//			maxHeightTitle = $(this).height();
	//	});
	//	$(this).find("div.article .title").height(maxHeightTitle);
	//
	//	var maxHeightSpecs = 0;
	//	$(this).find("div.article .details").each(function(){
	//		if($(this).height() > maxHeightSpecs)
	//			maxHeightSpecs = $(this).height();
	//	});
	//	$(this).find("div.article .details").height(maxHeightSpecs);
	//});

	var i = 0;
	$(".store_module .listing div.article").each(function(){
		if(i == 5){
			$(this).css('margin-right','0px');
			i = 0;
		}
		i++;
		$(this).find(".price_line:gt(0)").hide();
	});
	$(".store_module .listing .spec_group_menu select").change(function(){
		this.form.submit();
	});
	$(".store_module .item_details .article .title").wrapInner('<h1 class="title"></h1>');
	$(".shop_categories_module a,.shop_specifications_module a").removeClass('clear');
	
	//store aticle click
	$(".store_module .listing div.article").hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}		
	).click(function(e){
		var $target = $(e.target);
		if(!$target.parents('.ui').length)
			self.location.href = $(this).find(".thumb a").attr('href');
	});
	
	//order button
	$(".store_module div.ui .submit").mousedown(function(){
		o = $(this).parents('.ui').find('.input');
		if(!o.val())
			o.val(1);
	});

	//make every line same height
	if(!$("#middle .article .item_details").length){
		var templs = new Array();
			templs['default'] = 100;
			templs['half'] = 50;
			templs['thirth'] = 33;
			templs['quater'] = 25;
		var line_total = 0;
		var row_total = 0;
		var row_height = 0;
		$("#middle div.article.template").each(function(){
			nc = "";
			if($(this).next().length && $(this).next().next().length){
				nc = $(this).attr('class').split(' ')[1];
			}
			
			$(this).addClass('row_' + row_total);
	
			if($(this).height() > row_height){
				row_height = $(this).height();
			}
	
			line_total = line_total + templs[nc];
			if(line_total >= 100 || $("#middle .article").index(this) == ($("#middle .article").length - 1)){// || !nc){
				$(".article.template.row_" + row_total).height(row_height);
				$(".article.template.row_" + row_total + ":first").css('clear','left');
				row_height = line_total = 0;
				row_total++;
			}
		});
	}
	
	var make_same_height = function(selector){
		var max_height = 0;
		$(selector).each(function(){
			if($(this).height() > max_height){
				max_height = $(this).height();
			}
		});
		$(selector).height(max_height);	
	}
	
	make_same_height(".store_module .listing div.article .title");
	make_same_height(".store_module .listing div.article .details");
	make_same_height(".store_module .listing div.article .prices");
	make_same_height(".store_module .listing div.article");
});