$(window).load(function () {

	$(".font-face").css('visiblity', 'visible');

});

$(document).ready(function(){

	// cufon
	Cufon.replace('#introduction h1, #introduction h2, #introduction p, .page-title, #sidebar h6, #aside h6, .page_subtitle p', { fontFamily: 'PF Square Sans Pro Light'});
	Cufon.replace('#introduction h1 span', { fontFamily: 'PF Square Sans Pro Thin' });
	Cufon.replace('.live-chat p, .brochure p, .download, #main h1, .order-type, .page_subtitle p strong, #pageSlide h2, #pageSlide .emph, #pageSlide h4', { fontFamily: 'PF Square Sans Pro' });
	Cufon.replace('#introduction p a, #introduction .try-out, .section-col h6, .order-type .price, .order-type .price-alt, .price_inn, .add_cart, .feat-link, .demo-ss h4, .demo-two-col h4, .col-block h6, .box h4, #pageSlideSide h5', { fontFamily: 'PF Square Sans Pro Medium' });
	// $(".font-face").css('visiblity', 'hidden');

	$(".sub-block").append('<div class="clear"></div>');
	$("#nav > ul > li:has(ul) > a:first-child").addClass("hoverable");
	// $("#nav > ul > li:has(ul) a:first-child span").addClass("hoverable");


	if( $('.collapsed-list').length ) {
		$(".collapsed-list").each(function() {
			$(this).find("li:gt(2)").hide();
		})
	}


	$('.show-all a').each(function() {
		$(this).click(function() {
			if( $(this).parent().prev('.collapsed-list').find("li:hidden").length ) {
				$(this).parent().prev('.collapsed-list').find("li:hidden").slideDown();
				$(this).text("Show less").addClass('contract');
			} else {
				$(this).parent().prev('.collapsed-list').find('li:gt(2)').slideUp();
				$(this).text("Show more").removeClass('contract');
			}
			return false;
		})
	})

	// $('.show-less a').each(function() {
	// 	$(this).click(function() {
	// 		alert('mukodik');
			// $(this).parent().prev('.collapsed-list').find('li:gt(2)').hide();
			// $(this).text("Show more").parent().removeClass("show-less").addClass("show-all");
	// 		return false;
	// 	})
	// })

	$("#nav > ul > li:has(ul)").hover(
	      function () {
				$(this).addClass("hovered").find("ul").show();
	      },
	      function () {
				$(this).removeClass("hovered").find("ul").hide();
	      }
	    );


		equalHeight($(".col-block li"));

		$('.search-text-input').each(function() {

			var usedinput = $(this);
			var usedinputtitle = $(this).attr('title');

			$(this).click(remove)

			function remove() {
				if(usedinput.val() == usedinputtitle) {
					$(this).val("");
				}
			}

			$(this).blur(function() {
				if(usedinput.val() == "") {
					$(this).val(usedinputtitle);
					$(this).addClass('formError')
				} else {
					$(this).removeClass('formError')
				}
			}).focus(remove)


		});

		$('.NewsletterInputField').each(function() {

			var usedinput = $(this);
			var usedinputtitle = $(this).attr('title');

			$(this).click(remove)

			function remove() {
				if(usedinput.val() == usedinputtitle) {
					$(this).val("");
				}
			}

			$(this).blur(function() {
				if(usedinput.val() == "") {
					$(this).val(usedinputtitle);
					$(this).addClass('formError')
				} else {
					$(this).removeClass('formError')
				}
			}).focus(remove)


		});

		$('.pageSlideHolder').cycle({
			fx: 'fade',
			timeout: 0,
			speed:  700,
			containerResize: false,
			// fit: 1,
			prev: '#prevPageSlide',
		    next: '#nextPageSlide',
			pager: '.pager ul',
			pagerAnchorBuilder: function(idx, slide) {
		        return '<li><a href="#">' + idx +'</a></li>';
		    }

		    });

		var pagerWidth = $('.pager ul').width();
		$('.pager ul').css( 'margin-left', (718 - pagerWidth) / 2);

});

function equalHeight(group) {
         tallest = 0;
         group.each(function() {
             thisHeight = $(this).height();
             if(thisHeight > tallest) {
                 tallest = thisHeight;
             }
         });
         group.height(tallest);
     }

$("a[rel*=lightbox]").fancybox({
	'transitionIn'		: 'none',
	'transitionOut'		: 'none'
	//'titlePosition' 	: 'over',
	//'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
	//	return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
	//}
});

if(window.location.hash) {
	var relHash = window.location.hash.substr(1);
	$('#pageSlideSide ol li .active, #pageSlide .active').removeClass('active');
	$('#pageSlideSide ol .wrap[rel="' + relHash + '"], #pageSlide .' + relHash + '').addClass('active');

	var indexNr = $('#pageSlide .' + relHash).index();
	$('#pageSlide').css({ 'left': - indexNr * 560  })
}

$('#pageSlideHolder, #pageSlide').height($('#pageSlide .active').height());

$('#pageSlideSide ol .wrap').click(function() {

	var rel = $(this).attr("rel");
	var indexNr = $('.' + rel).index();
	var pageH = $('.' + rel).height();

	$('#pageSlideSide ol li .active, #pageSlide .active').removeClass('active');
	$(this).addClass('active');
	$('.' + rel).addClass('active');

	window.location.hash = rel;
	if ( $('#logo:above-the-top').size() ) {

		$('html, body').animate({ scrollTop: 0 }, function() {
	
			$('#pageSlideHolder, #pageSlide').height(pageH);
			$('#pageSlide').animate({ 'left': - indexNr * 560  })

		});

	} else { 

		$('#pageSlideHolder, #pageSlide').height(pageH);
		$('#pageSlide').animate({ 'left': - indexNr * 560  })

	}
	
	return false;
});


$('#nextSlidePag').click(function() {
	if( $('#pageSlide').position().left != "-2800" ) {
		if( $('#logo:above-the-top').size() ) {
			$('html, body').animate({ scrollTop: 0 }, function() {
				$('#pageSlide .active').removeClass('active').next().addClass('active');
				$('#pageSlideSide ol li .active').removeClass('active').parent().next().children().addClass('active');
				$('#pageSlideHolder, #pageSlide').height($('#pageSlide .active').height());
				$('#pageSlide').animate({ left: '-=560' });
			})

			return false;
		} else {
			$('#pageSlide .active').removeClass('active').next().addClass('active');
			$('#pageSlideSide ol li .active').removeClass('active').parent().next().children().addClass('active');
			$('#pageSlideHolder, #pageSlide').height($('#pageSlide .active').height());
			$('#pageSlide').animate({ left: '-=560' });
		}
		window.location.hash = $('#pageSlideSide ol li .active').attr('rel');
		return false;
	} else {
		// if it is the last slide	
		return false;
	}
});


$('#prevSlidePag').click(function() {
	if( $('#pageSlide').position().left != "0" ) {
		if( $('#logo:above-the-top').size() ) {
			$('html, body').animate({ scrollTop: 0 }, function() {
				$('#pageSlide .active').removeClass('active').prev().addClass('active');
				$('#pageSlideSide ol li .active').removeClass('active').parent().prev().children().addClass('active');
				$('#pageSlideHolder, #pageSlide').height($('#pageSlide .active').height());
				$('#pageSlide').animate({ left: '+=560' });
			})

			return false;
		} else {
			$('#pageSlide .active').removeClass('active').prev().addClass('active');
			$('#pageSlideSide ol li .active').removeClass('active').parent().prev().children().addClass('active');
			$('#pageSlideHolder, #pageSlide').height($('#pageSlide .active').height());
			$('#pageSlide').animate({ left: '+=560' });
		}
		window.location.hash = $('#pageSlideSide ol li .active').attr('rel');
		return false;
	} else {
		// if it is the first slide	
		return false;
	}
});

