//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){
	$title = obj;
	sIFR.replace(swiss, {
							elements: $title
							,wmode: 'transparent'
							,css: [
							  '.sIFR-root { font-size: ' + (parseInt($title.css('fontSize'))) + '; color: ' + hexed($title.css('color')) + '; }'
							  ,'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...
function popWindow(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();
	$("#modalContainer").css('height',h).css('width',w).css('margin-left',(w / 2) * -1); 
}

//change the contents of the current my_price_list
var change_my_price_list_amount = function(action,price){
	//change status
	$.getJSON('/domains/jasa.nl_2/modules/machines/my_price_list_agent.php', {a:action ? action : '', machineID:$('div.machine_details').attr('id').substring(10), priceID:price ? price : 0},function(json_data){

		if(json_data['alert'])
			alert('<div class="my_price_list_alert">' + json_data['alert'] + "</div>");

		//set tab amount
		if(json_data['selected_prices'].length)
			$("#tab_my_price_list .amount span").text(json_data['selected_prices'].length);		
			
		//reset checkboxes to right values...
		$(".machine_module .machine_details div.content_block .price_list div.price").each(function(){
			i = $(this).attr('id').substring(8);
			
			$("div.priceID_" + i).each(function(){
				o = $(this);
				d = o.find('div.description');
				
				//change state
				if($.inArray(i,json_data['selected_prices']) > -1){
					o.find('.checkbox').addClass('checked');
					if($.trim(d.text()) && !d.hasClass('open')){
						d.addClass('open').slideDown('normal',function(){
							if(price)
								$.scrollTo($("div.priceID_" + price),{offset:-100,duration:500});
						});
					}
				}else{
					o.find('.checkbox').removeClass('checked');
					if($.trim(d.text()) && d.hasClass('open')){
						d.removeClass('open').slideUp('normal',function(){
							if(price)
								$.scrollTo($("div.priceID_" + price),{offset:-100,duration:500});
						});
					}
				}
			});
		});

	});
}

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

//add price list events
var init_price_list = function(){
	//add hover and click to rows
	$(".machine_module .machine_details div.content_block .price_list div.price").hover(
		function(){$(this).find('.price_line').addClass('row_hover');},
		function(){$(this).find('.price_line').removeClass('row_hover');}
	).click(function(){
		//change checkbox
		if($(this).find('.checkbox').hasClass('checked')){
			//change amount of tab_my_price_list items
			$("#tab_my_price_list .amount span").html(parseInt($(".machine_module .machine_details div.content_block .price_list .checked").length));
			change_my_price_list_amount('del', $(this).attr('id').substring(8));
		}else{
			change_my_price_list_amount('add', $(this).attr('id').substring(8));
			s = Math.round(($(".machine_module .machine_details div.content_block .price_list div.price").index(this) ^ 3) * 100 + 500);
			$(this).find('.price_line:eq(0)').effect("transfer", {to: "#tab_my_price_list"}, s);
		}
	});
}


//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 + "\" 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 left colom?
	if($("#middle div.whole div.machine_module div.machine_listing").text()){
		$("#left").css('display','none');
		$("#middle").addClass('middle_3col');
	}

	//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');
	});
	
	//add big row css
	x = $(".machine_module .machine_details div.price_list").length ? ",.machine_module .machine_details div.content_block" : '';
	$(".dealer_module .dealer_row,.machine_module .machine_listing .machine_row:not(.3col_middle .machine_module .machine_listing .machine_row)" + x).wrapInner('<div class="gray_2col_top clear"><div class="gray_2col_bottom clear"><div class="gray_2col clear"></div></div></div>');
	$(".middle_3col .machine_module .machine_listing .machine_row").addClass('gray_3col').wrapInner('<div class="gray_3col_top clear"><div class="gray_3col_bottom clear"></div></div>');

	//change price list
	if($(".machine_module .machine_details div.price_list").length){

		//add tabs to machine info block...
		t = '<div class="tab tab_on" id="tab_product_info"><i>' + $(".machine_module .machine_details div.product_info").attr('id').replace(/[_]/g,' ') + '</i></div>';
		t+= '<div class="tab" id="tab_price_list"><i>' + $(".machine_module .machine_details div.price_list").attr('id').replace(/[_]/g,' ') + '</i></div>';
		t+= '<div class="tab" id="tab_my_price_list"><i>' + $(".machine_module .machine_details div.my_price_list").attr('id').replace(/[_]/g,' ') + ' <span class="amount">(<span>0</span>)</span></i></div>';
		$(".machine_module .machine_details div.content_block div.gray_2col_top").prepend('<div class="tabs clear">' + t + '</div>').parent().addClass('tabbed');

		//get current checked options and enable them...
		change_my_price_list_amount();

		//add click to tabs
		$(".machine_module .machine_details div.content_block .tabs .tab").click(function(){
			if(!$(this).hasClass('tab_on')){
				if($(this).attr('id') == 'tab_my_price_list'){
					$(".machine_module .machine_details div.content_block div." + $(this).attr('id').substring(4)).load('/domains/jasa.nl_2/modules/machines/price_list.php', {machineID:$('div.machine_details').attr('id').substring(10),my_price_list:true},function(){
						change_my_price_list_amount();
						init_price_list();

						//replace sIFR
						$(this).find('h1').each(function(){
							jquery_sifr_replacement($(this));
						});
					});
				}
				//highlight right tab...
				$(this).parent().find('.tab_on').removeClass('tab_on');
				$(this).addClass('tab_on');
				
				//change content...
				$(".machine_module .machine_details div.content_block div.tab_content:visible").slideUp('normal');
				$(".machine_module .machine_details div.content_block div." + $(this).attr('id').substring(4)).slideDown('normal');

				//sifr
				$('.machine_module .machine_details div.content_block h1').each(function(){
					jquery_sifr_replacement($(this));
				});					
			}
		});
		
		init_price_list();
	}
	
	//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,#wizard_ui h3,.machine_module .machine_details div.content_block h1').each(function(){
		jquery_sifr_replacement($(this));
	});
	
	//patch title margin
	$("div.title").css('padding-bottom','0');
	

	//change colom hights, do it at last so we have all hights...
	var f = 0;
	$("#middle .article_container").each(function(){
		if($(this).hasClass('colom')){
			f++;
		}else if($(this).hasClass('whole')){
			f = 0;
		}
		if(f == 2){
			$(this).addClass('right_colom');
			if($(this).find('.article').height() < $(this).prev().prev().find('.article').attr('height'))
				$(this).find('.article').height($(this).prev().prev().find('.article').attr('height'));
			else
				$(this).prev().prev().find('.article').height($(this).find('.article').height() + 3); //3px is FF patch!
			f = 0;
		}
	});

});
