$(window).load(function() {

	if( $('#bannerSpecialHolder img').size() ) {

	
		$('#bannerSpecialHolder').fadeIn();
	
	}

	$('#bannerSpecialHolder .close').click(function() {
	
		$(this).parent().hide();
		$('#intro-slide-nav ul li:first').trigger('click')
		return false;
	})

		$('#partners img, #prod-quick-list img').vAlign();

		if( $('#sidebar').height() > $('#main-content').height() ) {
	
		var sidebarH = $('#sidebar').height();
		//alert(sidebarH);
		 $('#main-content').height(sidebarH - 97);
	
	}

})

$(document).ready(function() {

Cufon.replace('h2', { fontFamily: "PF Square Sans Pro Light" });
Cufon.replace('h6', { fontFamily: 'PF Square Sans Pro Medium' });
Cufon.replace('h3', { fontFamily: 'PF Square Sans Pro' });
	
	// $('.tab-content').wrapInner("<ul></ul>");
	$('.tab-content > ul li:last').addClass('last');
	$('#tab-container').tabs();
	$('.ui-tabs-nav').wrap('<div id="tab-nav"></div>');
	$('#tab-nav').append('<div class="clear"></div>');
	$('.tab-content').wrapAll('<div id="tab-holder"></div>');

	$('.select-lang .caption').click(function() {
		$('.select-lang .option').slideDown()
	});
	
	
	$('.langSwitch').hover(function() {
	
		$(this).children('.options').fadeIn()
	
	}, function() {
	
		$(this).children('.options').fadeOut()
	
	})
	
	// $('.select-lang').click(function(){
	// 	$('.select-lang .option').slideDown();
	// }
	
	// $('.tab-content:first').show();

	equalHeight($("#tree-col .container-content"));

	// $('.pql-prod:last').after("</div>")
	
	// if ($('.pql-title').prev().is(".pql-prod")) {
	// 		$('.pql-title:not(:first)').before("</div>")
	// 	};
	// 			
	// 			if ($('.pql-prod').prev().is(".pql-title")) {
	// 						var aft = $('.pql-title').html();
	// 						$('.pql-title').html('</div>' + aft)
	// 					};
	
	// $('.intro-slide:first').css("display", "block");
	
	// if($("#sidebar").height() > $("#main").height()) {
	// 	$("#main-content").height($("#sidebar").height() - 460 )
	// } 

	$('.intro-slide').wrapAll('<div id="intro-slide-wrap"></div>');
	
	var elementTag        = $(".intro-slide"); // the element (child of the elementHolder)
	var elementHolder     = $("#intro-slide-wrap"); // holder of the elements (UL probably)

	var elementWidth      = 938; // width of the elementTag (px)
	var holderPosition    = 0; // 0
	var elementWidthTotal = elementTag.length * elementWidth; // calculates the width of all the elements

	elementHolder.width(elementWidthTotal); // adds the width to the elementHolder
	
	//	var autoslide = setInterval (function () {
		    
	//		var slideNr = $('#intro-slide-wrap').css('left'),
	//			slideNr = - (slideNr / 938px);

	//	console.log(slideNr);

		    
	//	}, 500);
			

	$('#intro-slide-nav li:first, .intro-slide:first').addClass('active');

//	if( $('#intro-slide-wrap .active').is(':first-child') ) {
//		console.log('first');
//	}

	function startSlide() { 
		
	 autoSlide = setInterval( function() {
	
	if( $('#intro-slide-wrap .active').is(':first-child') ) {
	
			// alert('first');
			$('.intro-slide.active, #intro-slide-nav .active').removeClass('active').next().addClass('active');
			elementHolder.animate({ left: - elementWidth });

		} else if( $('#intro-slide-wrap .active').is(':last-child') ) {
		
			// alert('last');
			$('.intro-slide.active, #intro-slide-nav .active').removeClass('active')
			$('.intro-slide:first, #intro-slide-nav li:first').addClass('active');
			elementHolder.animate({ left: 0 });

		} else {
		
			var activeSlide =  $('.intro-slide.active').index();
				activeSlide = activeSlide + 1;
		
			elementHolder.animate({ left: - elementWidth * activeSlide });
			$('.intro-slide.active, #intro-slide-nav .active').removeClass('active')
			$('.intro-slide:eq(' + activeSlide + '), #intro-slide-nav li:eq(' + activeSlide+ ')').addClass('active');
		} 
					
	}, 5000);
	}

	startSlide();

	$('#intro-slide-nav a').click(function() {
	
		if( $(this).parent().is('.active') ) {
		
			return false;

		} else {
		
			$('#intro-slide-nav li.active, .intro-slide.active').removeClass('active');
			$(this).parent().addClass('active');

			var indexNr = $(this).parent().index();

			$('.intro-slide:eq(' + indexNr + ')').addClass('active');
				elementHolder.animate({ left: - elementWidth * indexNr });

			clearInterval(autoSlide);
			startSlide();

			return false;

		}

	});

	//$('#intro-slide-nav a').each(function (i) {
	  //      $(this).click(function() {
		//		$('#intro-slide-nav .active').removeClass('active');
	      //      $(this).addClass('active');
			//	var rel = $(this).attr("rel");
	          //  var goTo = '#' + rel;
		//		var offset = $(this).parent().prevAll().length;
				// alert(offset);
		//		elementHolder.animate({left: - elementWidth * offset });

		//		clearInterval(autoslide);
				

		//		return false;         
	      //  });
	   // });

	$('#go-top').each(function (i) {
			       $(this).click(function() {
      		$('html, body').animate({scrollTop:0}, 'slow'); return false;
        });          
    });
	
	if ( $("#prod-quick-list").length > 0 ) {
//		var current = $("#prod-quick-list li.current").index();
		// $('#prod-quick-list').accordion({ header: '.pql-title' })

		$('#prod-quick-list .prod-list').hide();

		if( $('#prod-quick-list .prod-list .current').length ) {
			$('#prod-quick-list .prod-list .current').parent().show();
		} else {
			$('#prod-quick-list > div.prod-list:first').show();
		}

		$('.pql-title a').click(function() {
			$('#prod-quick-list .prod-list:visible').slideUp();
			$(this).parent().next().slideDown();
		
			return false;
		})

	}
	
	$('.partner:even').addClass('alt');
	if ( $(".partner").length > 1 ) {
		$('.partner:first').addClass('firstLine').next().addClass('firstLine');
	}
	
	
	
	if ( $("#prod-menu").length > 0 ) {
		
		$('#prod-menu ul').hide();

		if( $('#prod-menu ul .current').length ) {
			$('#prod-menu ul .current').parent().show();
		} else {
			$('#prod-menu > ul:first').show();
		}

		$('.pm-title a').click(function() {
			$('#prod-menu ul:visible').slideUp();
			$(this).parent().next().slideDown();
		
			return false;
		})

		//		alert(current);
		// $('#prod-menu').accordion({ header: '.pm-title', active: 0 });
//		var current = $("#prod-menu li.current").parent().index();
//		$('#prod-menu').accordion({ header: '.pm-title', active: current });
	}

	if ( $('.prod-list').length > 0) {
		$('.prod-list .pql-prod:last-child').addClass("last");
	}
	
	// if ( $(".pql-prod").length > 0 ) {
	// 		$(".pql-prod").wrapAll('<div></div>')
	// 	}
	
	function equalHeight(group) {
	         tallest = 0;
	         group.each(function() {
	             thisHeight = $(this).height();
	             if(thisHeight > tallest) {
	                 tallest = thisHeight;
	             }
	         });
	         group.height(tallest + 50);
	     }
	


$('input[type="text"], textarea').each(function() {

		var usedinput = $(this);		
		var usedinputtitle = $(this).attr('title');
	
		
		
		function addTitle() { 
			if(usedinput.val() == "") {
			$(usedinput).val(usedinputtitle);
		}};
		
		function remove() {
			if(usedinput.val() == usedinputtitle) {
				$(usedinput).val("");
			}
		}
		
		addTitle();

		$(this).blur(addTitle).focus(remove);
		
	});

$('#openComanda').click(function() {
	$('#formComanda').slideDown();
	return false;
})

	if(window.location.hash) {
		$('#formComanda').show();
	}


$('.selectCaption').live('click', function() {
			$(this).parent().find("ul").toggle();
			return false;
		});

		$('.selectelectList a').live('click', function() {
			var ul = $(this).parent().parent()

			var valueReplace =  $(this).html();
			ul.prev().html(valueReplace);

			ul.parent().prev().val($(this).find("span.value").html())

			ul.hide();

			return false;
		});

	$('.funkySelect').each(function() {

		// $(this).hide();
		var inputSelect = $(this);
		var selected = $(this).find("option[selected]");
		var options = $(this).find("option");

		var funkySelectHtml = '<div class="selectHolder"></div>';	
		$(this).after(funkySelectHtml);

		$(this).next('.selectHolder').append('<a class="selectCaption" href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a>');
		$(this).next('.selectHolder').append('<ul class="selectelectList"></ul>');

		options.each(function(){
			$(this).parent().next('.selectHolder').find('.selectelectList').append('<li><a href="#">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');
		});

		$(this).hide();

	});
		
});

