/*
 * removes flicker from ui tabs (plain crazy...)
 */

//var sbh_hide_timeout;

var keyvisualUp = false;
var tmpFoldBlock = '';
var initialFoldBlock;

function removeFlickerFromTabs(path) {
	$(""+path+" a").each(function(i) {	
		$(this).click(function() {							
				$(""+path+" a").each(function(i) {	
						x = this.href.split("#");
						$("#"+x[x.length-1]).addClass('ui-tabs-hide');
		        });

				t = this.href.split("#");
				$("#"+t[t.length-1]).removeClass('ui-tabs-hide');
			}); 
        });
}

/*
 * left menu logic
 */

$(function() {
	if($(".sbh")){
		$(".sbh").mouseout(
			function(){

			}
		);
	}
	
	
	
	/*
	 * Meniu
	 */
	
		if ($(".sb-bottom-content")) {
			var click_block = $(".sb-bottom-content");

			// alert(menu_link.addEvent());

			click_block.click(function () { 
				var menu_link = $(this).find("a");
		    	var link_to = menu_link.attr("href");
				window.location = link_to;
		
	    	});
		}
		if ($(".sb-top-block")) {
			var click_block = $(".sb-top-block");

			// alert(menu_link.addEvent());

			click_block.click(function () { 
				var menu_link = $(this).find("dl dt a");
		    	var link_to = menu_link.attr("href");
				window.location = link_to;
		
	    	});
	
		
	/* header image on mouse over fade in/out */
			startSlideShow();
			
			$('#pauseButton').click(function() { 
				$('#header_image').cycle('pause'); 
			
			});
			$('#resumeButton').click(function() { 
			    $('#header_image').cycle('resume'); 
			});
			$('#goto1').click(function() { 
			    $('#header_image').cycle(0); 
			    
			}); 
			$('#goto2').click(function() {  
			    $('#header_image').cycle(1);  
			    
			});
			$('#goto3').click(function() {  
			    $('#header_image').cycle(2);  
			    
			});
			$('#goto4').click(function() {  
			    $('#header_image').cycle(3);  
			 
			}); 
			

		
	}
	
	
	
	$('div.left_menu a').each(function(i)
	{	
		if (!($(this).parent().hasClass('cur') && $(this).parent().parent().parent().hasClass('left_menu')))
		{
			if (!$(this).parent().hasClass('cur')) {
				$(this).hover(
						function()  
						{  
							$(this).stop().css('backgroundColor', '#F3F3F3');
							$(this).stop().css('color', '#01AFDD');
						},
						function()  
						{
							textColor = $(this).hasClass('gray')?'#606060':'#000000'; 
							level2act = $(this).parent().parent().parent().hasClass('left_menu') && $(this).parent().hasClass('act');
							$(this).stop().animate({ backgroundColor: level2act?'#F3F3F3':'#ffffff', color: textColor}, 400, "easeInQuad", function()  
							{ 
								textColor = $(this).hasClass('gray')?'#606060':'#000000'; 
								level2act = $(this).parent().parent().parent().hasClass('left_menu') && $(this).parent().hasClass('act');
								
								if (!level2act)
							 		$(this).css("background-color", "ffffff");
								$(this).css("color", textColor);
							});
						});
			
			} else {
				$(this).hover(
						function()  
						{  
							$(this).stop().css('backgroundColor', '#F3F3F3');
							$(this).stop().css('color', '#01BAFE');
						},
						function()  
						{
							textColor = '#01BAFE'; 
							$(this).stop().animate({ backgroundColor: '#ffffff', color: textColor}, 400, "easeInQuad", function()  
							{ 
								textColor = '#01BAFE'; 
							 	$(this).css("background-color", "ffffff");
								$(this).css("color", textColor);
							});
						});
			}
		}
	});
	
});

/*
if (typeof sIFR == "object") {
	function forceRedraw() {
		sIFR.redraw();
		if (refreshNum++<20)
			setTimeout(forceRedraw, 500);
	}

	if (parseInt(sIFR.ua.ieVersion)>=7) {
		var refreshNum = 0;
		if (typeof swfobject == "object") {
			swfobject.embedSWFold = swfobject.embedSWF;
			swfobject.embedSWF = function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj)	{
				v = this.embedSWFold(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj);
				this.addDomLoadEvent(function() {
					forceRedraw();
				});				
				forceRedraw();
				return v;
			}
		}
		else
		{
			$(window).load(function() {
//				forceRedraw();
			});
			$(function() {
				forceRedraw();
			});
		}
	}
}
*/
var hvrd;

function headerImageOnLinks (img, effect, e) {
	$('div#header-image-mousever-div').css('display', 'block');
	var image_id = $('img#header-image-mouseover');
	if(hvrd != img){
		image_id.css('display', 'none');
	}
	image_id.attr('src',img);
	self.setTimeout(function(){	
		if (effect == 'none') {
			image_id.css('display', 'block');
		} else {
			image_id.fadeIn(750);
		}
	},200)
	hvrd = img;	
}

function fadeHeaderImage (obj,effect) {
	// console.log(hvrd == obj);
	if(hvrd != obj){
		setTimeout(function(){
			var image_id = $('img#header-image-mouseover');
			if (effect == 'none') {
				image_id.css('display', 'none');
			} else {
				image_id.fadeOut("slow");				
			}
		hvrd=false;
		},100)
	}
}

function setMeniuBlockHeight() {
		
		$('.sb-top-block').each(function(i) {
			var menu_block_dl = $(this).find("dl");
			var block_dl_h = $(menu_block_dl).height()
	
			$(menu_block_dl).css('margin-top', "-"+(block_dl_h)/2+"px");
		});
}
 
	
	


/*
 * search input logic
 */

function populateElement(selector, defvalue) {
	if($.trim($(selector).val()) == "") {
		$(selector).val(defvalue);
	}

	$(selector).focus(function() {
		if($(selector).val() == defvalue) {
			$(selector).val("");
		}
	});
    
	$(selector).blur(function() {
		if($.trim($(selector).val()) == "") {
			$(selector).val(defvalue);
		}
	});
}

$(function() {
	t = $('#search_sword').val();
	$('#search_sword').val("");
	populateElement('#search_sword', t);
});

/*
 * equal height logic
 */

$(function() {
	var currentSBH;
    $('div.grid').each(function(i)
	{	
		var g1 = $(this).find('div.section_bucket_inner:eq(0)');
		var g2 = $(this).find('div.section_bucket_inner:eq(1)');
		
		var a1 = g1.children('a[id]').length - g1.children('a[id][href]').length + g1.children('div.sbh').length;
		var a2 = g2.children('a[id]').length - g2.children('a[id][href]').length + g2.children('div.sbh').length;
		
		if (g1.children().length == 1+a1 &&
			g2.children().length == 1+a2)
		{
			if (g1.find('a.section').length==1 &&
				g2.find('a.section').length==1 &&
				!g1.find('a.section').hasClass('fixed_height') &&
				!g2.find('a.section').hasClass('fixed_height'))
			{
				h1 = g1.find('a.section').height();
				h2 = g2.find('a.section').height();
				
				if (h1<h2)
				{
					g1.find('a.section').height(h2);
				}
				else if (h2<h1)
				{
					g2.find('a.section').height(h1);
				}

				h1 = g1.find('a.section span').height();
				h2 = g2.find('a.section span').height();
				
				if (h1<h2)
				{
					g1.find('a.section span').height(h2);
				}
				else if (h2<h1)
				{
					g2.find('a.section span').height(h1);
				}
			}

			if (g1.find('a.dl_button').length==1 &&
				g2.find('a.dl_button').length==1)
			{
				pt1 = parseInt(g1.find('a.dl_button em').css('padding-top'));
				pb1 = parseInt(g1.find('a.dl_button em').css('padding-bottom'));
				
				pt2 = parseInt(g2.find('a.dl_button em').css('padding-top'));
				pb2 = parseInt(g2.find('a.dl_button em').css('padding-bottom'));
				
				h1 = g1.find('a.dl_button em').height();
				h2 = g2.find('a.dl_button em').height();
				
				if (h1<h2)
				{
					g1.find('a.dl_button em').css('padding-top', pt1+Math.floor((h2-h1)/2));
					g1.find('a.dl_button em').css('padding-bottom', pb1+Math.ceil((h2-h1)/2));
					g1.find('a.dl_button span.icon').css('top', Math.ceil(8+(h2)/2));
				}
				else if (h2<h1)
				{
					g2.find('a.dl_button em').css('padding-top', pt2+Math.floor((h1-h2)/2));
					g2.find('a.dl_button em').css('padding-bottom', pb2+Math.ceil((h1-h2)/2));
					g2.find('a.dl_button span.icon').css('top', Math.ceil(8+(h1)/2));
				} 
			}

			if (g1.find('div.feature').length==1 &&
				g2.find('div.feature').length==1)
			{
				h1 = g1.find('div.feature p').height();
				h2 = g2.find('div.feature p').height();
				
				if (h1<h2)
				{
					g1.find('div.feature p').height(h2);
				}
				else if (h2<h1)
				{
					g2.find('div.feature p').height(h1);
				} 
			}
		}
		
		$(this).find('.section_bucket').each(function(i){
			var sbh = $(this).find('.sbh');
			var sb_top = sbh.find('.sb-top');
			var section_height = $(this).find('.section').height();
			var em_text = $(this).find('.section em');
			var em_span = $(this).find('.section span');
			var section_header_height = $(this).find('.section em').height() + 18; // top and bottom padding
			
			sbh.find('.sb-bottom').click(function() {
				document.location.href = $(this).parent().parent().find('a.section').attr('href');
			});
			
			sbh.height(section_height + 268);
			sb_top.css('bottom', section_height);
			$(this).find('.section').mouseover(function(){
			 	keyvisualUp = true;				
				sbh.css('display', 'block');
				setMeniuBlockHeight();
				sb_top.animate({"height": "268px"});
				em_text.css('color', '#FFF');
				$('#header_image').cycle('pause'); 
				//$(this).addClass('t-hover');
		    });
			sbh.mouseover(function(){
				keyvisualUp = true;	
				stopTimer();
			}).mouseout(function(){
				if(initialFoldBlock){
					if(1 != $(this).data('isActive') && !$(this).hasClass('t-hover')){	
							keyvisualUp = false;				
							sbh_hide();
					}
				}else{
					keyvisualUp = false;				
					sbh_hide();					
				}
			});
			
			var sbh_hide_timeout;
			
			function sbh_hide(){
				sbh_hide_timeout = window.setTimeout(sbh_hide_time, 500);
			}
			
			function hide_sbh(){
				if(!keyvisualUp){
					$('img#header-image-mouseover').fadeOut("slow");
					$('div#header-image-mousever-div').css('display', 'none');
					$('#header_image').cycle('resume'); 
					keyvisualUp = false;
				}
			}
			
			function stopTimer() {
				window.clearTimeout(sbh_hide_timeout);
			}
			
			function sbh_hide_time(){
				sb_top.animate({"height": "0px"}, 300, 'linear',function(){sbh.css('display', 'none');});
				em_text.css('color', '#01afdd');				
				sb_top.parent().parent().removeClass('t-hover');
				window.setTimeout(hide_sbh, 500);
			}
			
			pImgArr = new Array();
			
			$(this).find('[onmouseover]').each(function(){
			 	var myImgSrc = this.getAttribute('onmouseover').toString();
			 		myImgSrc = myImgSrc.replace(/headerimageonlinks|\(|\'|\'|\)|\n|\r|function anonymous|\{|\}/gi,'');		
					myImgSrc = myImgSrc.substr(0,myImgSrc.indexOf(','));
					pImgArr= new Image(); 
					pImgArr.src=myImgSrc;
			})
			
		});
	});
});

$(document).ready(function(){
	var stack = [];
	var elements = $("#content_header_image img");

	for (var i = 0; i < elements.length; i++){
		var img = new Image();
		img.src = elements.eq(i).attr("src")
			// all img nodes (after the second) are removed from the DOM
			// once the corresponding image is loaded, it is pushed into the stack
		if (i > 0) {
			//elements.eq(i).remove();
			elements.eq(i).hide();
			$(img).bind("load",function(){
				stack.push(this);
				if (stack.length==elements.length-1)
						$("#content_header_image").cycle({
							fx:"fade",
							speed:1,
							timeout:  250
						});
			});
		}
	}
}); 

function startSlideShow(){
	$('#header_image').cycle({ 
	    fx: 'fade', 
	    speed: 2500
	 });
}

function initialFold(sbhParent){
	var sbh = sbhParent.find('.sbh')
	var sb_top = sbh.find('.sb-top');
	var section_height = sbhParent.find('.section').height();
	var em_text = sbhParent.find('.section em');
	var section_header_height = sbhParent.find('.section em').height() + 18; // top and bottom padding
	keyvisualUp = true;				
	sbh.css('display', 'block');
	setMeniuBlockHeight();
	sb_top.animate({"height": "356px"});
	em_text.css('color', '#FFF');
	$('#header_image').cycle('pause');
	sbh.addClass('t-hover');
}

function initialFoldOut(sbhParent){
	var sbh = sbhParent.find('.sbh');
	var sb_top = sbh.find('.sb-top');
	var em_text = sbhParent.find('.section em');		
	sb_top.animate({"height": "0px"});	
	window.setTimeout(function(){
		sbh.css('display', 'none');
		em_text.css('color', '#01afdd');
	}, 500);
	
}

$(function() {if(initialFoldBlock) initialFold(initialFoldBlock); });


function gotoStep(formRoot, step) {
	if (formRoot) {
		switch (step) {
			case '1':
				if (formRoot.children('div.requestform_step1'))
					formRoot.children('div.requestform_step1').css('display', 'block');
				if (formRoot.children('div.step2a'))
					formRoot.children('div.step2a').css('display', 'none');
				if (formRoot.children('div.step2b'))
					formRoot.children('div.step2b').css('display', 'none');
				break;
			case '2a':
				
				var table_html = '';
				var num = 0;

				if ((t1 = formRoot.children('div.requestform_step1')) &&
					(t2 = t1.children('div.clearfix')) &&
					(t3 = t2.children('div.requestform_step1_left')) &&
					(table = t3.children('div.table_placeholder'))) {
					table_html = table.html();
				}
				if ((u1 = formRoot.children('div.step2a')) &&
					(u2 = u1.children('div.table_placeholder'))) {
					
					if (table) {
						table_clone = table.clone(true);
						
						table.find("tr td.td-3 input").each(function(i) {	
							if(parseInt($(this).val())!=NaN &&
							   parseInt($(this).val())>0) {
									$(this).val(parseInt($(this).val()));
								} 
					        });
						
						table_clone.find("tr td.td-3 input").each(function(i) {	
							if(parseInt($(this).val())!=NaN &&
							   parseInt($(this).val())>0) {
									num++;
									$(this).replaceWith(parseInt($(this).val()));
								} else
								{
									while (true) {
										if ($(this).parent().parent().next().hasClass('spacer')) {
											$(this).parent().parent().next().remove();
											break;
										}
										$(this).parent().parent().next().remove();
									}
									$(this).parent().parent().remove();
								}
					        });

						u2.replaceWith(table_clone);
					}
					else
					{
						u2.html('');
					}
				}
				
				if (num==0) return gotoStep(formRoot, '2b');
				
				if (formRoot.children('div.requestform_step1'))
					formRoot.children('div.requestform_step1').css('display', 'none');
				if (formRoot.children('div.step2a')) {
					if (formRoot.find('div.step2a div.contact_placeholder div').length==0) {
						x = formRoot.children('div.step2b').children('div.contact_placeholder');
						y = formRoot.children('div.step2a').children('div.contact_placeholder');
						
						yy = y.clone(true);
						
						y.replaceWith(x.clone(true));
						formRoot.children('div.step2b').children('div.contact_placeholder').replaceWith(yy);
					}
					formRoot.children('div.step2a').css('display', 'block');
				}
				if (formRoot.children('div.step2b'))
					formRoot.children('div.step2b').css('display', 'none');
				break;
			case '2b':
				if (formRoot.children('div.requestform_step1'))
					formRoot.children('div.requestform_step1').css('display', 'none');
				if (formRoot.children('div.step2a'))
					formRoot.children('div.step2a').css('display', 'none');
				if (formRoot.children('div.step2b')) {
					if (formRoot.find('div.step2b div.contact_placeholder div').length==0) {
							x = formRoot.children('div.step2a').children('div.contact_placeholder');
							y = formRoot.children('div.step2b').children('div.contact_placeholder');
							
							yy = y.clone(true);
							
							y.replaceWith(x.clone(true));
							formRoot.children('div.step2a').children('div.contact_placeholder').replaceWith(yy);
						}
					formRoot.children('div.step2b').css('display', 'block');
				}
				break;
		}
	}
	return false;
}