// common js stuff...
$(document).ready(function(){

	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1)
			$(this).prev('br').css('display','none');
	});

	//change title height
	var titleHeight = 0;
	$(".catalog_module div.title").each(function(){
		if($(this).height() > titleHeight)
			titleHeight = $(this).height();
	});
	$(".catalog_module div.title").height(titleHeight);
	
	//rebuild store listing
	$(".catalog_module div.listing div.item").not(':hidden').slice(0,2).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(2,4).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(4,6).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(6,8).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(8,10).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(10,12).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(12,14).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(14,16).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(16,18).wrapAll('<div class="row clear"></div>');
	$(".catalog_module div.listing div.item").not(':hidden').slice(18,20).wrapAll('<div class="row clear"></div>');
	adjustListing();
	$(".catalog_module div.listing div.item div.image img").each(function(){
		x = (($(this).parents('div:eq(0)').height() - $(this).height()) / 2);
		$(this).css('margin-top',x + 'px');
	});
	
	//finish order
	obj = $('<a href="javascript:void(0)" class="mail"><span class="button_left"><span class="button_right"><span class="button_middle">Open het bestelformulier &#187;</span></span></span></a>').click(function(){
		$(this).hide();
		$(".catalog_module .selected_products .order").fadeIn();
	});
	$(".order_actions").append(obj);

	$(".order_actions").append($('<a href="/domains/ornamentenwinkel.nl/modules/catalogus/download.php" class="download"><span class="button_left"><span class="button_right"><span class="button_middle">Download uw geselecteerde producten&#187;</span></span></span></a>'));
	
	//show options
	$('.catalog_module div.item div.info_container').hover(
		function(){
			o = $(this).find('div.options');
			o.css('margin-top',(($(this).find('div.image').height()) * -1) + (o.height() / 2));
			o.fadeIn();
		},
		function(){
			o = $(this).find('div.options');
			o.fadeOut();
		}
	).click(function(){
		h = $(this).parents('.item').find('.more_info_link a').attr('href');
		self.location.href = h;
	});
	
	//checkbox
	$('.order_link a').each(function(){
		o = $('<div class="selected_box"></div>');
		
		if($(this).hasClass('checked'))
			o.addClass('checked');
		
		//add to page
		if($(this).parents('.item').length)
			$(this).parents('.item').find('div.title').prepend(o);
		else
			$(this).parents('.details').find('div.title').prepend(o);
	});

	//checkbox click
	$('.selected_box').click(function(){

		//get link
		if($(this).parents('.item').length)
			u = $(this).parents('.item').find('.order_link a').attr('href');
		else
			u = $(this).parents('.details').find('.order_link a').attr('href');

		//get id
		if(u.indexOf('order') > -1)
			id = u.match(/order=([0-9]+)/)[1];
		else
			id = u.match(/remove=([0-9]+)/)[1];

		//do ajax call
		var am;
		var s = Math.round($(this).offset().top);
		var parentItem = $(this).parents(".item");
		if($(this).hasClass('checked')){
			$(this).removeClass('checked');
			$.post('/domains/ornamentenwinkel.nl/modules/catalogus/agent.php',{remove:id},function(a){
				am = a;
				$("#tab_my_price_list .amount").html(' (' + am + ')');
				if(parentItem.length && !parentItem.parents('.selected_products').length)
					$("#tab_my_price_list").effect("transfer", {to: parentItem}, s);
			});
		}else{
			$(this).addClass('checked');
			$.post('/domains/ornamentenwinkel.nl/modules/catalogus/agent.php',{order:id},function(a){
				am = a;
				if(parentItem.length){
					parentItem.effect("transfer", {to: "#tab_my_price_list"}, s, function(){
						$("#tab_my_price_list .amount").html(' (' + am + ')');
					});
				}else{
					$("#tab_my_price_list .amount").html(' (' + am + ')');
				}
			});
		}

		//remove item?
		if($(this).parents('.selected_products').length && !$(this).hasClass('checked')){
			$(this).parents('.item').fadeOut(function(){
				$(this).remove();
				if(parseInt(am) == 0){
					self.location.href = self.location.href;
				}
			});
		}
	}).attr('title','Klik om dit product te (de)selecteren.');

	//more info links
	$(".catalog_module .more_info_link a").html('');
	$(".catalog_module div.item .actions").each(function(){
		o = $(this).parents('.item:eq(0)').find('.image a:eq(0)');
		a = o.attr('href');
		if(a){
			$(this).append($('<div class="magnify_link"><a href="' + a + '"></a></div>'));
			if(!$(this).parents('.selected_products').length)
				o.attr('href','javascript:void(0)');
		}
	});
	
	//amount change
	$(".amount .button").hide();
	$(".amount .input").change(function(){
		a = $(this).val();
		id = $(this).attr('name').match(/([0-9]+)/)[1];
		$.post('/domains/ornamentenwinkel.nl/modules/catalogus/agent.php',{order:id,amount:a});
	});

	$(".catalog_categories_module .category").click(function(){
		self.location.href = $(this).find('a:first').attr('href');
	}).find('img').each(function(){
		$(this).css('margin-top',50 - ($(this).height() / 2));
	});
	$(".catalog_categories_module .category:even").addClass('even');

	//build icons
	var files = new Array('ai','avi','bmp','cs','dll','doc','exe','fla','gif','jpg','js','mdb','pdf','png','ppt','rdp','txt','vsd','xls','xml','zip');
	$("#page a[href]").not("a[href$=php],a[href$=html]").each(function(){
		if($.trim($(this).text())){
			p = $(this).attr("href").split('.');
			e = p[p.length - 1].toLowerCase();
			for(i in files)
				if(files[i] === e)
					$(this).addClass('inline_icon icon_' + e);
		}
	});
	
	$("#left .shop_menu,#right ul").height($("#content").height());
	
	$("#tower").click(function(){
		popWindow('',645,400,$('#youtube'));
	});
	
	//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');
				if(u.indexOf('script')){
					u = $(this).attr('onclick').toString();
					u =  u.match(/"(\/[\d\w\/_\.]*)"/);
					if(u && u.length > 1)
						u = u[1];
				}
				e = '';
				if(u)
					e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e && 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 + ");");				
				}
			}
		}
	});
	
	//pop every alert
	$("div.alert").each(function(){
		str = $(this).html();
		alert(str);
	});


});

function adjustListing(){
	$(".catalog_module div.listing div.row").each(function(){
		$(this).find("div.item:first").addClass('margin');
		$(this).find("div.item:last").css('margin-right','0px');
	});	
}

//my own alert...
var alert = function(str){
	w = 200;
	h = 80;
	$('<div id="alert_popup clear">' + str + '</div>').css('width',w + 'px').css('height',h + 'px').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 + 'px').css('width',w + 'px').css('margin-left',((w / 2) * -1) + 'px');
	if(!$.browser.msie || ($.browser.msie && $.browser.version > 7))
		$("#modalContainer").css('top',(($(window).height() - h) / 2) + 'px'); 
}

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

//emulate popup function...
function popWindow(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(h > $(window).height())
			h = $(window).height() - 40;
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>");
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>");
	}
	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 + 'px').css('width',w + 'px').css('margin-left',((w / 2) * -1) + 'px');
	if(!$.browser.msie || ($.browser.msie && $.browser.version > 7))
		$("#modalContainer").css('top',(($(window).height() - h) / 2) + 'px'); 
}
