//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" 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); 
}

$(document).ready(function(){

/*	-------------------------------------------
	 CHANGE VALUE WHEN ORDERING
	----------------------------------------- */

	$('.spec_group_menus .spec_group_menu select').change(function(e){
		e.preventDefault();
		$(this).parents('form').trigger('submit');
	});

	$(".order_button input.submit").click(function(){
		o = $(this).parents('.ui').find('.amount .input');
		if(!o.val())
			o.val(1);
	});

/*	-------------------------------------------
	BE SOCIAL!
	----------------------------------------- */

	$('#socialfbplugin').html('<fb:like href="http://www.alittlecowsdream.nl" show_faces="false" width="340" font=""></fb:like>');

/*	-------------------------------------------
	QUOTE NEXT BUTTON
	----------------------------------------- */

	$(".quote_next_button").click(function(e){
		e.preventDefault();
		$.getJSON("/domains/alittlecowsdream.nl/modules/quotes/json.php",function(data){
			for(i in data){
				$(".quote_item h3.title").text(data[i].title);
				$(".quote_item .quote_message").html(data[i].message);
			}
			_typeface_js.renderDocument();
		});
		return false;
	});

/*	-------------------------------------------
	CHANGE SOME ARTICLES ON HOMEPAGE
	----------------------------------------- */

	$("#home #left-column > .article:eq(0)").addClass('first');
	$("#home #left-column > .article:eq(1)").addClass('second');
	$("#home #left-column > .article:eq(2)").addClass('thirth');
	$("#home #left-column div.article").css('border-bottom','0px');
	
	//$("#home #left-column .article.first").find('div.title h2.title').css('background','#ff0000');

/*	-------------------------------------------
	CHANGE SOLD OUT ARTICLES IN WEBSHOP
	----------------------------------------- */

	$(".sold_out_art").hover(
		function(){
			$(this).find('.overlay').hide();
		},
		function(){
			$(this).find('.overlay').show();
		}
	).click(function(){
		self.location.href = $(this).find('.thumb a:eq(0)').attr('href');
	});
	
/*	-------------------------------------------
	HIDE BR BEFORE MODULES / KILL EMPTY ARTICLES
	----------------------------------------- */
	
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	$('table.public_address tr:eq(10)').hide();

	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed,form,input,frame,.sIFR-replaced').length){
			$(this).remove();
		}
	});

/*	-------------------------------------------
	SHOP SLIDESHOW
	----------------------------------------- */
	
	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.show();//.fadeIn('slow');
				
				if($(this).hasClass('faded'))
					$(this).show(); //.fadeTo('slow',1);
			},
			function(){
				$('#shop_scroller .items .popInfoBox').remove();
				if($(this).hasClass('faded'))
					$(this).hide(); //.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)").outerWidth() * 4;
		$("#shop_scroller div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
			$("#shop_scroller div.scrollContent a.item:lt(4)").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)").outerWidth() * 4;
		i = $("#shop_scroller div.scrollContent a.item").length - 5;
		$("#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(){
		});
	});

	$(".store_module div.listing div.no_thumb span").each(function(){
		$(this).css({marginTop:62.5 - ($(this).outerHeight() / 2)});
	});

/*	-------------------------------------------
	CHANGE 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;
				
				var u = $(this).attr('href');
				var 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('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick');
	
					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('" + u + "'," + w + "," + h + ");");				
				}
			}

		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
			//get filename...
			u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/alittlecowsdream.nl/video.php?file=" + u + "',480,360);");		
		}
	});

	//RE RENDER DOCUMENT FOR TYPEFACE!
	_typeface_js.renderDocument();

});
