/**
 * @fileoverview jQuery scripts for http://hydraportal.pl/
 * @author Bartosz Białas
 * @copyright Copyright (c) Fresh Solutions Sp. z o.o.
 * @version 1.0
 */
 
	var colorboxAssigment = function() {
		
		$('.tabs-content').each(function(i) {
			$('.thumb a', this).colorbox({rel: 'group-' + i});
		});
		
	}
	
	var clientsFlashAsign = function() {
		
		if($('#flash-clients-list').size()) {
			if(!$('#flash-clients-list object').size()) {
				$('#flash-clients-list').append('<div id="flash-ani-list"></div>');
				swfobject.embedSWF("<##HOST##>/pub/uploadflash/slider.swf", "flash-ani-list", "908", "440", "9.0.28.0", "<##HOST##>/pub/uploadflash/expressInstall.swf", {host : "<##HOST##>", mode : 'full', hGap: 55, vGap : 30, cols : 6}, {wmode:"transparent"});
			}
		}
		
	}
	
$(function(){
	
	$.fn.colorbox.settings.opacity = '0.7';
	$.fn.colorbox.settings.previous = '&lt; poprzednie';
	$.fn.colorbox.settings.next = 'następne &gt;';
	$.fn.colorbox.settings.close = 'zamknij';
	$.fn.colorbox.settings.current = '{current} z {total}';
	
	$('a.demo-link').click(function() {
		_gaq.push(['_trackPageview', "/linki/demo/demo-hydraportal-pl"]);
		
	});
	
	$('#try-hp').click(function() {
		_gaq.push(['_trackEvent', 'button', 'press', 'zobacz_mozliwosci']);
		//pageTracker._trackEvent('button', 'press', 'zobacz_mozliwosci');
	});
	$('#latest-implementations .more a').click(function() {
		_gaq.push(['_trackEvent', 'button', 'press', 'wiecej_informacji_o_serwisach']);
		//pageTracker._trackEvent('button', 'press', 'wiecej_informacji_o_serwisach');
	});
	$('#partnership-conditions .more a').click(function() {
		_gaq.push(['_trackEvent', 'button', 'press', 'sprawdz_warunki_wspolpracy']);
		//pageTracker._trackEvent('button', 'press', 'sprawdz_warunki_wspolpracy');
	});
	
	/**
	 * Home flash animation
	 */
	if($('#promo-ani-flash').size()) {
		$('#promo-ani-flash').append('<div id="flash-object"></div>');
		swfobject.embedSWF("<##HOST##>/pub/uploadflash/cu3er.swf", "flash-object", "620", "370", "9.0.28.0", "<##HOST##>/pub/uploadflash/expressInstall.swf", {xml : "<##HOST##>/pub/xml/config.xml"}, {wmode:"transparent"});
	}
	
	/**
	 * Flash clients
	 */
	if($('#flash-clients').size()) {
		$('#flash-clients').append('<div id="flash-ani"></div>');
		swfobject.embedSWF("<##HOST##>/pub/uploadflash/slider.swf", "flash-ani", "950", "50", "9.0.28.0", "<##HOST##>/pub/uploadflash/expressInstall.swf", {host : "<##HOST##>", gap: 20}, {wmode:"transparent"});
	}
	
	/**
	 * Flash clients sub
	 */
	if($('#flash-clients-sub').size()) {
		$('#flash-clients-sub').append('<div id="flash-ani"></div>');
		swfobject.embedSWF("<##HOST##>/pub/uploadflash/slider.swf", "flash-ani", "480", "190", "9.0.28.0", "<##HOST##>/pub/uploadflash/expressInstall.swf", {host : "<##HOST##>", mode : 'rect', hGap: 20, vGap : 20}, {wmode:"transparent"});
	}
	
	/**
	 * Newsletter button hover effect
	 */
	$('#newsletter-form button').hover(function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	/**
	 * Main-nav animation
	 */
	$('#main-nav > li')
		.mouseover(function(){
			$(this)
				.filter(':not(".active")')
				.find('a')
					.stop()
					.animate({opacity: 1}, {duration: 150});
		})
		.mouseout(function(){
			$(this)
				.filter(':not(".active")')
				.find('a')
					.stop()
					.animate({opacity: 0}, {duration: 150});
		});
	
	/**
	 * Portfolio ajax load
	 */
	$('.tabs a').click(function() {
		var tabnfo = $(this).text();
		
		_gaq.push(['_trackPageview', "/przykladowe-www/" + tabnfo]);
		// pageTracker._trackPageview('/przykladowe-www/' + tabinfo);
	
		$('.tabs a').removeClass('active');
		$(this).addClass('active');
		id = $(this).parent().attr('id').split('-');
		if($('#tc-' + id[1]).size()) {
			$('#tc-' + id[1]).show().siblings('.tabs-content').hide();
		} else {
			$('.tabs').after('<div id="tc-' + id[1] + '" class="tabs-content"></div>');
			$('#tc-' + id[1]).siblings('.tabs-content').hide().end().addClass('processing').load($(this).attr('href') + '&filter=portfolio', function() {
				$('#tc-' + id[1]).removeClass('processing').show();
				colorboxAssigment();
				clientsFlashAsign();
			});
		}
		return false;
	});
	
	/**
	 * Portfolio gallery, clients assigment
	 */
	colorboxAssigment();
	clientsFlashAsign();
	
	$("a[rel='portfolio-gallery']").colorbox();
	
	
	
	/**
	 * Get HP ajax forms
	 */
	$('#partner a, #become-partner a, #get-hp a.partner, #get-hp a.implementation, #implementation a').click(function() {
		$(this).colorbox({href:$(this).attr('href') + '?filter=contact'});
	});
	
	$('.cboxcontact, #footer .cboxcontact').colorbox(
		{	
			onComplete: function(){
				$('#cboxLoadedContent #contact-form').submit(function(){
					mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
					$form = $(this);
					$form.find('.warning').removeClass('warning');
					if ($.trim($('#f_name').val()) == '')
						$('label[for="f_name"]').parent('div').addClass('warning');
					if ($.trim($('#f_who').val()) == '-1')
						$('label[for="f_who"]').parent('div').addClass('warning');
					if (!mailFilter.test($.trim($('#f_mail').val())))
						$('label[for="f_mail"]').parent('div').addClass('warning');
					if ($.trim($('#f_msg').val()) == '')
						$('label[for="f_msg"]').parent('div').addClass('warning');
					if ($('.warning').size()){
						alert('Wypełnij poprawnie wszystkie obowiązkowe pola formularza (oznaczone gwiazdką).');
						$('.warning:first label').next().focus();
						return false;
					}
					$form.addClass('processing').find('#unique_id').val((Math.random()) * 10);
					$.post('<##HOST##>/admin/forms/form_submit.php', $form.find('input, textarea, select').serialize(), function(){
						if($('#f_who').val() == 'Webmaster') {
							_gaq.push(['_trackPageview', "/formularz/partnerzy/wyslany"]);
						} else if($('#f_who').val() == 'Właściciel strony www') {
							_gaq.push(['_trackPageview', "/formularz/klienci/wyslany"]);
						} else {
							_gaq.push(['_trackPageview', "/formularz/inni/wyslany"]);
						}
						$.post('<##HOST##>/showpage.php?pageID=28', $form.find('input').serialize(), function(){
							$form.removeClass('processing').html('<div class="success"><p><strong>Dziękujemy, formularz kontaktowy został wysłany.</strong></p></div>');
						});
					});
					return false;
				});			
			}
		}
	);
	
	
	/**
	 * Implementation form validation
	 */
	$('#implementation-form button').live('click', function(event){
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$form = $('#implementation-form');
		$form.find('.warning').removeClass('warning');
		if ($.trim($('#f_name').val()) == '')
			$('label[for="f_name"]').parent('div').addClass('warning');
		if (!mailFilter.test($.trim($('#f_mail').val())))
			$('label[for="f_mail"]').parent('div').addClass('warning');
		if ($('.warning').size()){
			alert('Wypełnij poprawnie wszystkie obowiązkowe pola formularza (oznaczone gwiazdką).');
			$('.warning:first label').next().focus();
			return false;
		}
		$form.addClass('processing').find('#unique_id').val((Math.random()) * 10);
		$.post('<##HOST##>/admin/forms/form_submit.php', $form.find('input, textarea').serialize(), function(){
			_gaq.push(['_trackPageview', "/formularz/klienci/wyslany"]);
			//pageTracker._trackPageview("/kontakt-wyslany-klient.html");
			$.post('<##HOST##>/showpage.php?pageID=28', $form.find('input').serialize(), function(){
				$form.removeClass('processing').html('<div class="success"><p><strong>Dziękujemy, formularz został wysłany.</strong></p></div>');
			});
		});
		
		return false;
	});
	
	/**
	 * Partner form validation
	 */
	$('#partner-form button').live('click', function(event){
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$form = $('#partner-form');
		$form.find('.warning').removeClass('warning');
		if ($.trim($('#f_name').val()) == '')
			$('label[for="f_name"]').parent('div').addClass('warning');
		if (!mailFilter.test($.trim($('#f_mail').val())))
			$('label[for="f_mail"]').parent('div').addClass('warning');
		if ($('.warning').size()){
			alert('Wypełnij poprawnie wszystkie obowiązkowe pola formularza (oznaczone gwiazdką).');
			$('.warning:first label').next().focus();
			return false;
		}
		$form.addClass('processing').find('#unique_id').val((Math.random()) * 10);
		$.post('<##HOST##>/admin/forms/form_submit.php', $form.find('input, textarea').serialize(), function(){
			_gaq.push(['_trackPageview', "/formularz/partnerzy/wyslany"]);
			//pageTracker._trackPageview("/kontakt-wyslany-partner.html");
			$.post('<##HOST##>/showpage.php?pageID=28', $form.find('input').serialize(), function(){
				$form.removeClass('processing').html('<div class="success"><p><strong>Dziękujemy, formularz został wysłany.</strong></p></div>');
			});
		});
		
		return false;
	});
	
	/**
	 * Capabilities List
	 */
	if($('#capabilities-list').size()) {
		$('dl').find('dd')
			.hide()
			.end().show()
			.find('dt').each(function() {
				$(this).wrapInner('<a href="#"></a>');
				$(this).find('a')
					.click(function(e){
						if ($(this).attr('class') == 'active') {
								$(this)
									.parent()
									.next('dd').hide();
								$(this)
									.removeClass();
							}
							else {
								$(this)
									.addClass('active');
								$(this)
									.parent()
										.next('dd').show();
							}
							e.preventDefault();
					});
			});
		$('#capabilities-list').show();
	}
	
	/**
	 * Partners slide list
	 */
	$('#partners-list h3').each(function() {
		$(this).wrapInner('<a href="#"></a>');
	});
	$('#partners-list li ul').hide();
	$('#partners-list').show();
	
	$('#partners-list h3 a').click(function(e){
		if ($(this).attr('class') == 'active') {
			$(this)
				.parent()
					.next('ul').hide();
					$(this)
						.removeClass();
		}
		else {
			$(this)
				.addClass('active');
			$(this)
				.parent()
					.next('ul').show();
		}
		e.preventDefault();
	});
	
	/**
	 * Hosting list layer
	 */
	$('#requirements .show-hosting').colorbox();
	
	/**
	 * Price list more layer
	 */
	$('.price-box .more a, .price-box h3 a').colorbox();

	/**
	 * Blog archive slide up/down
	 */
	$('#show-more a').toggle(function() {
			$('#archive-entries').show();
			$(this).text('Zwiń pozostałe wpisy').addClass('close');
			return false;
		}, 
		function() {
			$('#archive-entries').hide();
			$(this).text('Pokaż pozostałe wpisy').removeClass('close');
			return false;
		});
		
	/**
	 * Newsletter
	 */
	var $dir = $('#newsletter-form input:text[value]');
	var startVal = $dir.val();
	$dir.focus(function(){
		(this.value == startVal) && $dir.val('');
	})
	.blur(function(){
		(!this.value) && $dir.val(startVal);
	});
	
	$('#newsletter-form').submit(function(){
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (!mailFilter.test($.trim($('#mail', this).val()))){
			alert('Podaj swój adres e-mail.');
			$(this).find('input').addClass('warning').focus();
			return false;
		}
	});
	
	/**
	 * Comment form validation
	 */
	$('#news-comments form').submit(function() {
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$form = $(this);
		$form.find('.warning').removeClass('warning');
		if ($.trim($('#annotation_content').val()) == '')
			$('label[for="annotation_content"]').parent('div').addClass('warning');
		if ($.trim($('#annotation_personalData').val()) == '')
			$('label[for="annotation_personalData"]').parent('div').addClass('warning');
		if (!mailFilter.test($.trim($('#annotation_email').val())))
			$('label[for="annotation_email"]').parent('div').addClass('warning');
		if ($('.warning').size()){
			alert('Wypełnij poprawnie wszystkie obowiązkowe pola formularza (oznaczone gwiazdką).');
			$('.warning:first label').next().focus();
			return false;
		}
	});
	
	/**
	 * Contact form validation and submit
	 */
	$('#contact-form').submit(function(){
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$form = $(this);
		$form.find('.warning').removeClass('warning');
		if ($.trim($('#f_name').val()) == '')
			$('label[for="f_name"]').parent('div').addClass('warning');
		if ($.trim($('#f_who').val()) == '-1')
			$('label[for="f_who"]').parent('div').addClass('warning');
		if (!mailFilter.test($.trim($('#f_mail').val())))
			$('label[for="f_mail"]').parent('div').addClass('warning');
		if ($.trim($('#f_msg').val()) == '')
			$('label[for="f_msg"]').parent('div').addClass('warning');
		if ($('.warning').size()){
			alert('Wypełnij poprawnie wszystkie obowiązkowe pola formularza (oznaczone gwiazdką).');
			$('.warning:first label').next().focus();
			return false;
		}
		$form.addClass('processing').find('#unique_id').val((Math.random()) * 10);
		$.post('<##HOST##>/admin/forms/form_submit.php', $form.find('input, textarea, select').serialize(), function(){
			if($('#f_who').val() == 'Webmaster') {
				_gaq.push(['_trackPageview', "/formularz/partnerzy/wyslany"]);
			} else if($('#f_who').val() == 'Właściciel strony www') {
				_gaq.push(['_trackPageview', "/formularz/klienci/wyslany"]);
			} else {
				_gaq.push(['_trackPageview', "/formularz/inni/wyslany"]);
			}
			$.post('<##HOST##>/showpage.php?pageID=28', $form.find('input').serialize(), function(){
				$form.removeClass('processing').html('<div class="success"><p><strong>Dziękujemy, formularz kontaktowy został wysłany.</strong></p></div>');
			});
		});
		return false;
	});
	
	/**
	 * jCarouselLite Homepage Gallery Overview
	 */
	if($('#portfolio-overview').size()) {
		$('#portfolio-overview div').show().jCarouselLite({
			auto: 4000,
			btnNext: '.next',
			btnPrev: '.prev',
			visible: 1
		});
		$('#portfolio-overview ul').css('visibility', 'visible');
	}
	
	/**
	 * jCarouselLite example www gallery overview
	 */
	if($('#example-www-overview').size()) {
		$('#example-www-overview div').show().jCarouselLite({
			auto: 4000,
			btnNext: '.next',
			btnPrev: '.prev',
			visible: 1
		});
		$('#example-www-overview ul').css('visibility', 'visible');
	}
	
	/**
	 * Google map
	 */
	if ($('#map').size() && GBrowserIsCompatible()) {
		lat = 52.405665;
		lng = 16.936111;
		infoText = $('#map-content').html();
		$('#map-content').remove();
		var hpIcon = new GIcon(G_DEFAULT_ICON);
		hpIcon.image = "<##HOST##>/pub/images/marker.png";
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(lat, lng), 14);
		var latlng = new GLatLng(lat, lng);
		marker = new GMarker(latlng, {icon: hpIcon});
		GEvent.addListener(marker, "click", function(){
			map.openInfoWindowHtml(latlng, infoText);
		});
		map.openInfoWindowHtml(latlng, infoText);
		map.addOverlay(marker);
	}
	
	
	/**
	 * Slide Widget
	 */
	
	
	$(window).scroll(function()
		{
			var $this = $("#slide-widget");
			var windowHeight = $(window).height();
			var triggerHeight = windowHeight * .5;
			
			if(window.pageYOffset > triggerHeight) {
				$this.removeClass('hide').addClass('show');
				$this.fadeIn();
			} else {
				$this.removeClass('show').addClass('hide');
				$this.fadeOut();
			}				
		}
	);
	
	
});
