

//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;overflow:hidden;\"><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.data.addClass('alert');
					dialog.container.css('height','auto');
					dialog.overlay.fadeIn('normal', function () {
						dialog.container.show('normal', function () {
							dialog.data.slideDown('normal'); // See Other Notes below regarding
													   // data display property and
													   // iframe details
						});
					});
				},
		onClose: function (dialog) {
					dialog.data.slideUp('normal', function () {
					  dialog.container.hide('normal', function () {
						dialog.overlay.fadeOut('normal', function () {
						  $.modal.close(); // must call this to have SimpleModal
										   // re-insert the data correctly and
										   // clean up the dialog elements
						});
					  });
					});
				}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}

//scroll header items
var SGtmr = new Array();
var scrollHeaderItems = function(sel){
	x = $(sel + ':eq(0)').width() * -1;
	$(sel + ':eq(0)').animate({marginLeft:x + 'px'},'slow',function(){
		$(sel + ':eq(0)').css('margin-left','0px').appendTo($(sel).parent());
		$(sel + ':eq(0)').css('margin-left','0px');
		if(SGtmr[sel])
			clearTimeout(SGtmr[sel]);
		SGtmr[sel] = setTimeout('scrollHeaderItems("'+sel+'")',6000);
	});
}	

//make same height
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);	
}



$(document).ready(function(){
	//scrollHeaderItems('#header .best_sold_module ol li');
	scrollHeaderItems("#header div.random .random_article");
	
	$('#header div.random .random_article img').each(function(){
		$(this).attr('src',$(this).attr('src').replace('/thumb.','/preview.')).removeAttr('align').removeAttr('width').removeAttr('height');
	});

	//fix banner left side...
	$(window).bind('resize', function() {
		$("#body .body_side").width(($("#body").width() - $("#container").width()) / 2);
	});
	$(window).trigger('resize');
	
	//kill empty titles and articles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text())){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').css('display','none');
		}
	});

	//change menu's
	$("#menu > ul > li:first,#topmenu > ul > li:first,#bottommenu > ul > li:first").addClass('first');

	//open/close submenu
	$("#menu ul li,.shop_pricesmenu_module ul li,.shop_specifications_module ul li,.shop_categories_module ul li").hover(
		function(){
			if($(this).children('ul:eq(0)').is(':hidden'))
				$(this).children('ul:eq(0)').slideDown('fast');
		},
		function(){
			if($(this).children('ul:eq(0)').is(':visible') && !$(this).hasClass('hold'))
				$(this).children('ul:eq(0)').slideUp('fast');
		}
	);
	
	//make manufacturer module
	if($("#middle div.shop_manufacturers_module").length){
		var s = $('<select />').change(function(){
			self.location.href = $(this).val();
		}).appendTo($("#middle div.shop_manufacturers_module"));
		s.append($('<option />').text('-- selecteer fabrikant --'));
		$("#middle div.shop_manufacturers_module ul li a").each(function(){
			s.append($('<option />').val($(this).attr('href')).text($(this).text()));
		});
		$("#middle div.shop_manufacturers_module ul").remove();
	}

	//open current submenu
	if($("#menu a.current_buttonID").length){
		$("#menu a.current_buttonID").parents('li:last').addClass('hold').trigger('mouseover');
	}	

	//change all popups...
	var img = new Array();
	$("#middle .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 + ");");				
				}
			}
		}
	});

	$(".store_module .listing div.article").addClass('article_container').addClass('clear');
	$(".store_module .listing .specs").addClass('clear');	
	//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++;
			}
		});
	}
	
	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");
});
