//common js stuff


//config sIFR
var Dekodisplay = {
	src: '/domains/podiumvictorie.nl_2/fonts/Dekodisplay-Bold.swf'
};
sIFR.useStyleCheck = true;
sIFR.activate(Dekodisplay);

/* == Converts color to HEX == */
var hex = function(N) {
	if(N==null) return "00";
	N = parseInt(N);
	if(N==0 || isNaN(N)) return "00";
	N = Math.max(0, N);
	N = Math.min(N, 255);
	N = Math.round(N);
	return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
};
/* == Converts colors to HEX == */
var hexed = function(color) {
	if(!color) { return false; }
	if(color.search('rgb') > -1) {
		color = color.substr(4,color.length-5).split(', ');
		color = hex(color[0]) + hex(color[1]) + hex(color[2]);
	}
	color = color.replace('#','');
	if(color.length < 6) {
		color = color.substr(0, 1) + color.substr(0, 1) + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1);
	}
	return '#' + color;
};

//my own routine to do a calculated sifr replacement.
var jquery_sifr_replacement = function(obj,wm){
	$title = obj;
	sIFR.replace(Dekodisplay, {
							elements: $title
							,wmode: wm ? '' : 'transparent'
							,css: [
							  '.sIFR-root {background-color: ' + hexed($title.css('backgroundColor')) + '; font-size: ' + (parseInt($title.css('fontSize'))) + '; color: ' + hexed($title.css('color')) + '; text-transform: ' + $title.css('text-transform') + '; }'
							  ,'a { text-decoration: none; color: ' + hexed($title.css('color')) + '; text-transform: ' + $title.css('text-transform') + '; }'
							  ,'a:hover { color: ' + hexed($title.css('color:hover')) + '; text-transform: ' + $title.css('text-transform') + '; }'
							]
						});
}

//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('fast', function () {
							dialog.container.show('fast', function () {
								dialog.data.slideDown('fast'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('fast', function () {
						  dialog.container.hide('fast', function () {
							dialog.overlay.fadeOut('fast', 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); 
}


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

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


	//kill empty titles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed').length){
			$(this).remove();
		}
	});
	
/*
	var u = "/domains/podiumvictorie.nl_2/video/promo.flv";
	$.getJSON('/domains/podiumvictorie.nl_2/video/get_flv_info_agent.php',{file:u,json:true},function(data){
		c = $('a').css('color');
		$('div.girl').width(350).append('<div style="display:block;float:right;"><a href="javascript:popWindow(\'/domains/podiumvictorie.nl_2/video/index.php?file=' + u + '\','+ data['video']['resolution_x'] +','+ data['video']['resolution_y'] +');"><div class="title"><h1 class="title" style="color:'+c+'">Promo Video</h1></div><img src="/domains/podiumvictorie.nl_2/images/promo_video.jpg" /></a></div>');
		jquery_sifr_replacement($("#footer h1"));
	});
*/

	//do sIFR replacements
	$(".photoalbum_module .albumsets .set .added").remove();
	$('.photoalbum_module .albumsets .set .name,.article h1,#login h1,.news_module div.title,.program_module div.month > div.title,.program_module div.overview div.title').each(function(){ //,.headline_module ul li,#left .highlights_module ul li
		jquery_sifr_replacement($(this));
	});	
	//hide blank br
	$(".article .content .module_contents,#middle > .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	//main button select
	$('#menu .current_buttonID').parents('li').addClass('current_buttonID').find('a').addClass('current_buttonID');
	//make images of menu
	$('#menu ul li a').not($("#menu ul ul li a")).each(function(){
    	t = $(this).html();
		if(t){
			
			$(this).html("<img class=\"buttonImg\" src=\"/domains/podiumvictorie.nl_2/generate/button.php?txt=" + t + ($(this).is(".current_buttonID") ? "&current=true" : "")+ "\" border=\"0\" />");
	
			//hover
			$("#menu img.buttonImg").parent('a').hover(
				function(){
					if(!$(this).children('.buttonImg').attr('origSrc'))
						$(this).children('.buttonImg').attr('origSrc',$(this).children('.buttonImg:eq(0)').attr('src'));
	
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s + '&over=1');
				},
				function(){
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s);
				}
			);
		}
    });
	//submenu animation
 	var zIndex = 30000;
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden')){
				  $(this).children('ul:eq(0)').css('z-index',zIndex++).slideDown('normal');
			 }
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible'))
				  $(this).children('ul:eq(0)').slideUp('normal');
			 }
	);
	
	//flicker popup UI
	$(".flickralbum_module ul.thumbs li a").click(function(e){
		var currImgIndx = $(this).parents('ul.thumbs').find('a').index(this);
		
		o = $('<div id="flickr_popup_ui"><div class="image"><div class="back"></div><div class="forward"></div></div></div>');
		o.find('.image').css({backgroundImage:'url(' + $(this).attr('href') + ')'}).hover(
			function(){$(this).find('div').fadeIn('normal');},
			function(){$(this).find('div').fadeOut('normal');}
		);

		o.find('.forward').click(function(){
			currImgIndx++;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		o.find('.back').click(function(){
			currImgIndx--;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		
		popWindow('',504,350,o);
		
		return false;
	});


	//kill MS word stuff!
	$(".MsoNormal,.MsoNormal *").removeAttr('style');

	//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('" + 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').indexOf('module=flash_flv_player') > -1 || $(this).attr('href').match(/.(flv|FLV)$/))){
			//get filename...
//			u = $(this).attr('href').match(/(file=).*(.flv)/)[0];
//			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/jasa.nl_2/popup/video.php?" + u + "',300,240);");		

			var u = $(this).attr('href');
			var o = $(this);
			$.getJSON('/domains/podiumvictorie.nl_2/video/get_flv_info_agent.php',{file:u,json:true},function(data){
				o.addClass('popWindowLink').attr('href','javascript:popWindow(\'/domains/podiumvictorie.nl_2/video/index.php?file=' + u + '\','+ data['video']['resolution_x'] +','+ data['video']['resolution_y'] +')');
			});

		}
	});
	
	//my own alert...
	var alert = function(str){
		$('<div id="alert_popup clear"><span class="string">' + str + '</span></div>').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); 
	}
	
	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

	//check
	$(".photoalbum_module .albumsets .set").click(function(){
		self.location.href = $(this).find('a:eq(0)').attr('href');
	}).hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}
	);

	//program row hover
	$(".program_module .listing div.row").hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}
	);
	
	//archive
	$(".program_archive_module li.artist").click(function(){
		if($(this).children('ul:eq(0)').is(':hidden')){
			$(".program_archive_module li.artist ul:visible").slideUp('normal');
			$(this).children('ul:eq(0)').slideDown('normal');
		}else{
			$(this).children('ul:eq(0)').slideUp('normal');
		}
	});

	if($("#comming_up").length){
		$("#comming_up .overlay .next_week_module div.overview:lt(3)").wrapAll('<div class="row clear"></div>');
		$("#comming_up .overlay .next_week_module div.overview:gt(2)").not("#comming_up .overlay .next_week_module div.overview:gt(5)").wrapAll('<div class="row clear"></div>');
		$("#comming_up .overlay .next_week_module div.overview:gt(5)").wrapAll('<div class="row clear"></div>');
		
		var comminTmr;
		var comming_up_animate = function(){
			$("#comming_up .overlay .next_week_module .row:eq(0)").animate({marginTop:'-174px'},'normal',function(){
				$("#comming_up .overlay .next_week_module .row:eq(0)").css({marginTop:'0px'}).appendTo($("#comming_up .overlay .next_week_module"));
				$("#comming_up .overlay .next_week_module .row:eq(0)").css({marginTop:'0px'});
				if(comminTmr)
					clearTimeout(comminTmr);
				comminTmr = setTimeout(comming_up_animate,10000);
			});		
		}
		comminTmr = setTimeout(comming_up_animate,10000);
	}

});