//enable firebug
var console;

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

//dummy
var popInfo = function(){}

//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(){
//$(function() {
	
	//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 all movie files
		}else if($(this).attr('href') && $(this).attr('href').indexOf('module=flash_flv_player') > -1){
			//get filename...
			u = $(this).attr('href').match(/(file=).*(.flv)/)[0];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/blend.nl/popup/video.php?" + u + "',300,240);");		
		}
	});

	//make room for right col
	if($("#right .article.default").length){
		if($.browser.version < 7 && $.browser.msie)
			$("#middle").css('float','left').width(580);
		else
			$("#middle").css('float','left').width(585);
	}

	//change first article(s)
	if($("#middle .article:eq(0)").hasClass('default')){
		$("#middle .article:eq(0)").addClass('noline');
	}else{
		$("#middle .article:eq(0)").addClass('first');
		for(i=0;i<3;i++){
			if($("#middle .article:eq(" + i + ")").hasClass('thirth')){
				$("#middle .article:eq(" + i + ")").addClass('noline');
			}else if($("#middle .article:eq(0)").hasClass('default')){
				break;
			}
		}
	}
	if(!$("#middle .article:eq(0)").hasClass('thirth') && $("#middle .article:eq(1)").hasClass('thirth')){
		$("#middle .article:eq(1)").addClass('first');
		for(i=1;i<4;i++){
			if($("#middle .article:eq(" + i + ")").hasClass('thirth')){
				$("#middle .article:eq(" + i + ")").addClass('noline');
			}else if($("#middle .article:eq(0)").hasClass('default')){
				break;
			}
		}
	}
	if($("#right .article:eq(0)").hasClass('default')){
		$("#right .article:eq(0)").addClass('noline');
	}
	$("#content .article.default").each(function(){
		if($(this).next('.article').hasClass('thirth'))
			$(this).next('.article').addClass('first');
	});
	
		
	//change shop scroller.
	$("#shop_scroller").append('<div class="popInfoBox"></div>');
	$("#shop_scroller div.items").prepend('<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();
	$("#shop_scroller .items div.right").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").width() * 9;
		$("#shop_scroller div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
			$("#shop_scroller div.scrollContent a.item:lt(9)").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() * 9;
		i = $("#shop_scroller div.scrollContent a.item").length - 10;
		$("#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;
		i = o = s = null;
		$("#shop_scroller .categories a.current_categoryID").removeClass('current_categoryID');
		$(this).addClass('current_categoryID');
		h = $(this).attr('href');
		if(h.indexOf('categoryID') > 0)
			i = h.match(/categoryID=(\d+)/)[1];
		if(h.indexOf('on_sale') > 0)
			o = parseInt(h.match(/on_sale=(\d+)/)[1]);
		if(h.indexOf('shopID') > 0)
			s = h.match(/shopID=(\d+)/)[1];
		
		url = '/domains/sagra.nl/modules/bottle_listing/listing.php?shopID=' + s + '&' + (o ? 'on_sale=1' : 'categoryID=' + i);
		
		$("#shop_scroller div.items .scrollContent").load(url,shop_scroller_AddItemHover);

		return false;
	});
	
	
	//rebuild store listing
	$(".store_module .listing div.article").addClass('article_container');
	$(".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);
	});
	$(".store_module .listing div.article").each(function(){
		$(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);
	});	

	//regiomap module
	$(".regiomap_module area").tooltip({
		track: true,
		positionRight: true ,
		delay: 0,
		showURL: false,
		top:-40,
		left:-20
	});
	$('.regiomap').maphilight({
		fill: true,
		fillColor: 'dd0000',
		fillOpacity: 0.5,
		stroke: true,
		strokeColor: 'ffffff',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: true,
		alwaysOn: false
	});
	$(".regiomap_module ul li a").hover(
		function(){
			n = $(this).find(".name").text().replace(/\W/g,'_').toLowerCase();
			$(".regiomap_module area#" + n).trigger('mouseover');
		},
		function(){
			$.tooltip.blocked = false;
			n = $(this).find(".name").text().replace(/\W/g,'_').toLowerCase();
			$(".regiomap_module area#" + n).trigger('mouseout');
		}
	);
	if($(".store_module .item_details").length){
		n = $(".store_module .item_details .spec_item_1 .spec label").text().replace(/\W/g,'_').toLowerCase();
		if(n)
			$('<img src="/domains/sagra.nl/modules/map/info/' + n + '.png" alt="" border="0" class="regio_details_map" />').appendTo('.store_module .item_details');
		
		$(".store_module").hover(
			function(){
				$.tooltip.blocked = true;
				n = $(".store_module .item_details .spec_item_1 .spec label").text().replace(/\W/g,'_').toLowerCase();
				$(".regiomap_module area#" + n).trigger('mouseover');
			},
			function(){
				$.tooltip.blocked = false;
				n = $(".store_module .item_details .spec_item_1 .spec label").text().replace(/\W/g,'_').toLowerCase();
				$(".regiomap_module area#" + n).trigger('mouseout');
			}
		);
	}
});
