/**
* Common UI stuff...
*	by me@daantje.nl
*/


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

var foundOnloadAlerts = new Array();
var alert = function(str){
	foundOnloadAlerts[foundOnloadAlerts.length] = str;
}

//start scripts
$(document).ready(function(){

	/* SUB MENUS */
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden'))
				 $(this).children('ul:eq(0)').slideDown('normal');
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible'))
				 $(this).children('ul:eq(0)').slideUp('normal');
		 }
	);

	/* Images */
	$("#middle .no_line .article .content").each(function(){
		$(this).find("img:eq(0)").prependTo($(this).parents('div.article'));
	});
	$("#middle .weggeefacties_module .item .content").each(function(){
		$(this).find("img:eq(0)").prependTo($(this).parents('div.item'));
	});
	$("#middle .article img").each(function(){
		if(!$(this).parents('.social_bookmarks').length){
			if($(this).parent().is('a')){
				$(this).removeAttr('align');
				$(this).parent().wrap('<div class="image_border leftFloatImg clear"></div>');
			}else{
				$(this).removeAttr('align').wrap('<div class="image_border leftFloatImg clear"></div>');
			}
		}
	});
	$("#left .article img").each(function(){
		if(!$(this).parents('.social_bookmarks').length){
			if($(this).parent().is('a')){
				$(this).removeAttr('align');
				$(this).parent().wrap('<div class="image_border rightFloatImg clear"></div>');
			}else{
				$(this).removeAttr('align').wrap('<div class="image_border rightFloatImg clear"></div>');
			}
		}
	});
	$(".news_module .news_article").each(function(){
		i = $(this).find("img:eq(0)");
		if(i.length && !i.parents('.category_3').length){
			h = i.attr('src');
			if(h.indexOf('130x170_Thumb.') < 0 && i.height() > 170 && i.width() < 500 && $(".news_module .news_article").length > 2){
				var p = h.match(/(.*\/).*\..*/)[1];
				var f = h.replace(/(.*\/)/,'130x170_Thumb.');
				
				i.parent('.image_border').addClass('loading news_thumbnail');
				imgID = 'imgID_' + uid();
				i.addClass(imgID);
				
				$.post("/domains/blend.nl/generate/news_thumbnail.php",{p:p,f:f,o:h.replace(/(.*\/)/,''),w:130,h:170,i:imgID},function(imgID){
					$('.' + imgID).css('display','none');//.src = p + f;
					$('<img src="' + p + f + '" width="130" height="170" alt="" />').appendTo($('.' + imgID).parent('.image_border'));
					$('.' + imgID).parent('.image_border').removeClass('loading');
				});
			}else if(i.width() >= 500){
				i.parent('.image_border').css('width',(i.width() + 2) + 'px').wrap('<div class="centerImage clear"></div>');
			}
		}
	});
	
	//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);");		
		}
	});

	//social boormarks
	$(".social_bookmarks .icons a:gt(7)").wrapAll('<div class="more_bookmark_icons" style="display:none;overflow:hidden;width:637px;clear:both;"></div>');
	$(".social_bookmarks .icons a:eq(7)").after('<div class="more_bookmark_link"><a href="javascript:more_bookmark_icons()" class="more_bookmarks">More &#187;</a></div>');

	//rebuild blend.tv listing
	o = $(".news_module .listing.category_3 div.article_container");
	if(o.length){
		o.slice(0,3).wrapAll('<div class="row clear"></div>');
		o.slice(3,6).wrapAll('<div class="row clear"></div>');
		o.slice(6,9).wrapAll('<div class="row clear"></div>');
		o.slice(9,12).wrapAll('<div class="row clear"></div>');
		$(".news_module .listing.category_3 .row").each(function(){
			$(this).find("div.article_container:last").css('margin-right','0');
		});
		$(".news_module .listing.category_3 .row .article").each(function(){
			$(this).find(".leftFloatImg").css('cursor','pointer').click(function(){
				self.location.href = $(this).parents('.article').find("a.title").attr('href');
			}).each(function(){
				i = $(this).find('img:eq(0)');

				var s = i.attr('src');
				var id = 'tvthumb_' + uid();
				i.attr('id',id);
				img[id] = new Image();
				img[id].onload = function(){
					img[id].onload = null;
					w = img[id].width;
					h = img[id].height;

					$('img#' + id).wrap('<div style="background-image:url('+ img[id].src +');" class="play_overlay clear"><div class="play_button" style="width:' + w + 'px;height:' + h + 'px;"></div></div>');
				}
				img[id].src = s;
			});
		});
	}	
	//change blend.tv articles
	o = $(".news_module .detail.category_3 div.title:first")
	if(o.length){
		o.before($(".news_module .detail.category_3 div.option_bar"));
		o.prepend('<h1 class="rightTitle">Blend.TV</h1>');
		o.after($(".news_module .detail.category_3 .news_body:first div.module_contents").addClass('blendTV_player clear'));
		$(".news_module .detail.category_3 h1.title").each(function(){
			if($(this).text() == 'Relevante artikelen'){
				t = $(this).text().replace(/artikelen/,'video\'s');
				$(this).text(t);
			}
		});
	}
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

	//Last line
	o = $("#footer").prev(':last').find('div.article:last');
	if(o.css('background-image'))
		o.css({'background-image':'none','padding-bottom':'0'});

	//get latest cover
//	$("#menu ul li a").each(function(){
//		if($(this).text() == 'Covers / Nabestellen'){
//			$(this).append('<div class="latest_cover image_border rightFloatImg clear"></div>');
//			$("#menu ul li div.latest_cover").load('/domains/blend.nl/modules/covers/latest.php').css('cursor','pointer').click(function(){
//				self.location.href = $(this).parent().attr('href');
//			});
//		}
//	});
	

	//my own alert...
	var alert = function(str){
		$('<div id="alert_popup clear">' + str + '</div>').css('height','52px').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('width','300px').css('height','70px').css('margin-left',(300 / 2) * -1);
	}
	
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
});

//interact with player, auto start on load...
var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	player.sendEvent('PLAY');
}


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



//animate social bookmarks
var more_bookmark_icons = function(){
	$('.social_bookmarks .icons .more_bookmarks').css('display','none');
	$(".social_bookmarks .icons div.more_bookmark_icons").slideDown('slow');
}
