//common js stuff


//config sIFR
var swiss = {
	src: '/domains/jasa.nl_2/fonts/Swiss721_CnItBt.swf'
	,ratios: [8, 1.41, 9, 1.33, 10, 1.35, 15, 1.31, 19, 1.28, 20, 1.27, 28, 1.26, 29, 1.24, 30, 1.25, 40, 1.24, 43, 1.23, 44, 1.24, 64, 1.23, 72, 1.22, 73, 1.23, 115, 1.22, 116, 1.21, 119, 1.22, 121, 1.21, 1.22]
};
sIFR.useStyleCheck = true;
sIFR.activate(swiss);

/* == 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(swiss, {
							elements: $title
							,wmode: wm ? '' : 'transparent'
							,css: [
							  '.sIFR-root {display: block; background-color: ' + hexed($title.css('backgroundColor')) + '; font-size: ' + (parseInt($title.css('fontSize'))) + '; color: ' + hexed($title.css('color')) + '; text-align: ' + $title.css('text-align') + '}'
							  ,'a { text-decoration: none; color: ' + hexed($title.css('color')) + '; }'
							  ,'a:hover { color: ' + hexed($title.css('color:hover')) + '; }'
							]
						});
}

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

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

//my own alert...
var alert = function(str){
	$('<div id="alert_popup"><div class="top"></div><div class="middle">' + str + '</div><div class="bottom"></div></div>').modal();
	$("#modalContainer").css('width','350px').css('margin-left',(350 / 2) * -1); 
}

//start script
var global = this; 
$(document).ready(function(){

	/* MENU BUTTONS */
	$('#menu ul li a').not($("#menu ul ul li a")).each(function(){
    	t = $(this).html();
		if(t){
			
			$(this).html("<img class=\"buttonImg\" src=\"/domains/jasa.nl_2/generate/button.php?txt=" + t + ($("#header").is(":visible") ? "&home=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);
				}
			);
		}
    });

	/* SUB MENUS */
	$("#menu ul ul").wrapInner('<div class="submenu_bottom clear"><div class="submenu clear"><div class="submenu_top clear"></div></div></div>');
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden'))
				 $(this).children('ul:eq(0)').css('display','block');
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible'))
				 $(this).children('ul:eq(0)').css('display','none');
		 }
	);
	
	//kill extra lines....
	$("#menu li:first, #topmenu li a:first, #menu ul ul a:first, #bottommenu a:first").css('border','0px');
	$(".colom .news_module .content:last").addClass('last_news_article');
	
	//extra menu icons.
	$("#topmenu li:first a,#bottommenu li:first a").each(function(){
		t = $(this).text().toLowerCase();
		if(t.indexOf('rss') > -1)
			t = 'rss';
		if(t == 'dutch' || t == 'english' || t == 'rss')
			$(this).addClass('icon_' + t);
	});
	
	//change image margins...
	$(".article div.content img").each(function(){
		if($(this).attr('align') == 'left')
			$(this).addClass('right_margin');
		else if($(this).attr('align') == 'right')
			$(this).addClass('left_margin');
	});
	
	//change submits...
	$("input.submit,input.button").wrap('<div class="submit_border"></div>');

	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

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

	//do sIFR replacements
	$('.article h1,#left .news_module div.title,.machine_module .machine_details div.content_block h1,.machine_listing h2').each(function(){
		jquery_sifr_replacement($(this));
	});
	$("#wizard_ui h3").each(function(){
		jquery_sifr_replacement($(this),true);
	});
	
	//patch title margin
	$("div.title").css('padding-bottom','0');
	

	//change colom hights, do it at last so we have all hights...
	var f = 0;
	var last_height = 0;
	var found_row = 0;
	$("#middle .article_container").each(function(){
		$(this).addClass('colom_row_' + found_row);
		if($(this).hasClass('colom')){
			f++;
			if($(this).find('.article').height() > last_height)
				last_height = $(this).find('.article').height();
		}else if($(this).hasClass('whole')){
			f = 0;
			found_row++;
		}
		if(f == 2){
			if($(this).find('.article').height() > last_height)
				last_height = $(this).find('.article').height();

			$(this).addClass('right_colom');
			$('.colom_row_' + found_row).find('.article').height(last_height);
			last_height = f = 0;
			found_row++;
		}
	});

});