(function($){
    $.fn.removeOrphans = function(){
    	return this.each(function(i) {
	        if($(this).length > 0) {
	            var $html = $(this).html();
	            $html = $html.replace(/(\s)([\S])[\s]+/g, "$1$2&nbsp;");
	             $(this).empty().html($html);
	        }
 		});
    }
})(jQuery);


//general
Cufon.replace('.content h1', {fontFamily: 'PF Black'});
Cufon.replace('.content h2', {fontFamily: 'Politica Light'});
Cufon.replace('.content h3', {fontFamily: 'Politica Light'});

//header
Cufon.replace('.top .mainMenu ul li a span', {fontFamily: 'Politica'});


// footer
Cufon.replace('.footer h3', {fontFamily: 'Politica Light'});
Cufon.replace('.footer .menu ul li a span', {fontFamily: 'Politica'});

//portfolio
Cufon.replace('.portfolioDetailsText h1', {fontFamily: 'Politica Light'});
Cufon.replace('.portfolioDetailsText .projectInfo ul li strong ', {fontFamily: 'Politica'});
Cufon.replace('.portfolioDetailsText .projectInfo ul li span', {fontFamily: 'Politica'});
Cufon.replace('.portfolioDetailsText .projectInfo ul li a', {fontFamily: 'Politica'});

//uslugi
Cufon.replace('.servicesInfo h3, .servicesInfo h4', {fontFamily: 'Politica'});
Cufon.replace('.servicesMenu ul li a span', {fontFamily: 'Politica'});

//kontakt
Cufon.replace('.contactPerson h4', {fontFamily: 'Politica'});

//home
Cufon.replace('.content span.day', {fontFamily: 'Politica Light'});

//blog
Cufon.replace('.postMeta h4', {fontFamily: 'Politica'});
Cufon.replace('.postCategories p', {fontFamily: 'Politica'});
Cufon.replace('h4#comments, h4#respond', {fontFamily: 'Politica'});

$(document).ready(function(){     
	
	
	$('.wysiwyg, .servicesInfo, .portfolioDescription, .aboutCompanyText, .raportContent').removeOrphans();
	
	function checkCareerUrl(){
		var url = document.location.href.split('#')[1];
		if (url == 'kariera') {
			$('#menu-o-nas').removeClass('active');
			$('#menu-kariera').addClass('active');
		}
	}
	
	checkCareerUrl();
	
	function checkPortfolioUrl(){
		var url = document.location.href;
		if ( url.search('portfolio') > 0 ) {
			$('#menu-portfolio').addClass('active');
		}
	}
	
	checkPortfolioUrl();
	
	$('#kariera').click(function(){checkCareerUrl();});
	
  // gallery scroller
  	if($('.galleryScrollBox').length>0){
	    var intOverallDelta = 0;
		 
	    var galleryScrollBoxHeight =  $('.galleryScrollBox').height();
	    var galleryScrollBoxHeight_1 =  $('.galleryScrollBox .gallerySliceScrollBox:eq(0) .gallerySliceScroller').height()-galleryScrollBoxHeight;
	    var galleryScrollBoxHeight_2 =  $('.galleryScrollBox .gallerySliceScrollBox:eq(1) .gallerySliceScroller').height()-galleryScrollBoxHeight;
	    var galleryScrollBoxHeight_3 =  $('.galleryScrollBox .gallerySliceScrollBox:eq(2) .gallerySliceScroller').height()-galleryScrollBoxHeight;
		 
	    var galleryScrollBoxProportion_1 = galleryScrollBoxHeight_1/galleryScrollBoxHeight; 
	    var galleryScrollBoxProportion_2 = galleryScrollBoxHeight_2/galleryScrollBoxHeight; 
	    var galleryScrollBoxProportion_3 = galleryScrollBoxHeight_3/galleryScrollBoxHeight; 	  
		  
	    $('.galleryScrollBox').mousewheel(function(event, delta) {
	      	if (delta > 0){           
	        	if(intOverallDelta>=0) return false;     
				intOverallDelta+=10;                                                                                                                     			        
				$('.galleryScrollBox .gallerySliceScrollBox:eq(0) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_1)+'px');			        
				$('.galleryScrollBox .gallerySliceScrollBox:eq(1) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_2)+'px');			        
				$('.galleryScrollBox .gallerySliceScrollBox:eq(2) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_3)+'px');							
			}
			else if (delta < 0){ 
	       		if(galleryScrollBoxHeight_1<=(intOverallDelta*galleryScrollBoxProportion_1*-1))  return false;   
				intOverallDelta-=10;   		   		        
				$('.galleryScrollBox .gallerySliceScrollBox:eq(0) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_1)+'px');
				$('.galleryScrollBox .gallerySliceScrollBox:eq(1) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_2)+'px');
				$('.galleryScrollBox .gallerySliceScrollBox:eq(2) .gallerySliceScroller').css('top', (intOverallDelta*galleryScrollBoxProportion_3)+'px');
			}    						
			event.preventDefault();
		});
	}    				
	// end gallery scroller		
	
  // main page banner slider  
	$('.mainPageBannerContener').slides({
  		fadeSpeed: 300,
		play: 4000,
		pause: 0,
		hoverPause: true,
		effect: 'fade',
		prev: 'prev',
		next: 'next'
	});
	// end main page banner slider  
	
	// main menu
 /* $(".top .mainMenu > ul > li").hover(
    function () {
      if($(this).find('.subMenuText').length>0){
        $(this).addClass('hover');
        $(this).find('.subMenuText').clone().appendTo('.subMenuContener .subMenuContent .subMenuContentText');
        $('.subMenuContener').show();
      }
    }, 
    function (e) {
      var tempElement = $(e.relatedTarget);
      if(tempElement.parents('.subMenuContener').hasClass('subMenuContener')){return false;}
            
      $(this).removeClass('hover');
      $('.subMenuContener').hide();
      $('.subMenuContener .subMenuContent .subMenuContentText').empty();
    }
  );
  
  $(".subMenuContener").hover(
    function () {
    }, 
    function () {
      $(".top .mainMenu > ul > li").removeClass('hover');
      $(this).hide();
      $(this).find('.subMenuContentText').empty();
    }
  );
	*/
  // end main menu

  
	/*$(".top .mainMenu > ul > li").hover(
	    function () {
	    	$(this).addClass('hover');
	    }, 
	    function () {
	      $(".top .mainMenu > ul > li").removeClass('hover');
	    }
	);*/



//news
	$('.mainPageNews .itemList li').hover(
		function () {	
			$(this).find('.newsHover').fadeIn();
			//$(this).addClass('hover');
		},
		function (e) {
			$(this).find('.newsHover').fadeOut('fast');
			//$(this).removeClass('hover');
		}
	);

//end news


  // clients
	$('.content .clientsContent ul.itemList .about').hover(
		function () {
			var offset = $(this).parent().offset();
			
			$('.clientHoverContener').css({'top' : offset.top});
			$('.clientHoverContener .clientHoverColor').animate({'left' : offset.left}, 200);
			 
			$(this).animate({opacity: 1.0});
			$('.clientHoverContener').show();
		},
		function (e) {
			var tempElement = $(e.relatedTarget);
			$(this).animate({opacity: 0.0}, 200);
			  
			$('.clientHoverContener').hide();
		}
	);

	// end clients
               
	// about us scroller
	staffScrollActiveItem = 1;            
	    
	var scrollContenerLenght = $(".content .staffScroll").width();
	var scrollButtonsLenght = $(".content .staffScroll .scrollButtons").width();
	var scrollCountItem = ($('.content .staffList ul li').length)-1;
	var scrollLenghtItem = ((scrollContenerLenght-scrollButtonsLenght)/scrollCountItem);
     
	$(".staffScroll span.next").click(function() { 
    	$(".staffScroll span").removeClass('disabled');            
    	if(staffScrollActiveItem<$('.content .staffList ul li').length){    
      		staffScrollActiveItem++;
      		getNextPerons(staffScrollActiveItem);               
      		$(".content .staffScroll .scrollButtons").animate({
      			left: (scrollLenghtItem*(staffScrollActiveItem-1))+'px'},
      			1750,
        		function() {
          			if(staffScrollActiveItem==$('.content .staffList ul li').length){$(".staffScroll span.next").addClass('disabled');}
        		}       
      		);                    
    	}                                      
	});
  
  	$(".staffScroll span.prev").click(function() {
    	$(".staffScroll span").removeClass('disabled'); 
    	$('.content .aboutStaff .staffDetails').children().stop(true, true);
    	if(staffScrollActiveItem>1){    
      		staffScrollActiveItem--;
      		getPrevPerons(staffScrollActiveItem);                                                                              
      		$(".content .staffScroll .scrollButtons").animate({left: (scrollLenghtItem*(staffScrollActiveItem-1))+'px'}, 1750,
        		function() {
          			if(staffScrollActiveItem==1){$(".staffScroll span.prev").addClass('disabled');}
        		}       
      		);
    	}                                      
  	});

  	function getNextPerons(index){
	    $('.content .aboutStaff .staffDetails img').animate({left: '442px'}, 800);
    	weit(50);   
	    $('.content .aboutStaff .staffDetails h3').animate({left: '442px'}, 800);     
	    weit(50);   
	    $('.content .aboutStaff .staffDetails .postion').animate({left: '442px'}, 800);   
	    weit(50);   
	    $('.content .aboutStaff .staffDetails p').animate({left: '442px'}, 800,
			function() {
	      		var newDetails = $('.content .staffList ul li:eq('+(index-1)+')').html();
	       	 	$('.content .aboutStaff .staffDetails').html(newDetails);    
	               
	        	$('.content .aboutStaff .staffDetails').children().css('left', '-442px');        
	                                          
	        	$('.content .aboutStaff .staffDetails img').animate({left: '0px'}, 800);
	        	weit(50);   
		        $('.content .aboutStaff .staffDetails h3').animate({left: '0px'}, 800);     
		        weit(50);   
		        $('.content .aboutStaff .staffDetails .postion').animate({left: '0px'}, 800);   
		        weit(50);   
		        $('.content .aboutStaff .staffDetails p').animate({left: '0px'}, 800);      
	        
			}    
		);
  	}
  
  	function getPrevPerons(index){
    	$('.content .aboutStaff .staffDetails img').animate({left: '-442px'}, 800);
    	weit(50);   
    	$('.content .aboutStaff .staffDetails h3').animate({left: '-442px'}, 800);     
	    weit(50);   
	    $('.content .aboutStaff .staffDetails .postion').animate({left: '-442px'}, 800);   
	    weit(50);   
	    $('.content .aboutStaff .staffDetails p').animate({left: '-442px'}, 800,
      		function() {
		        var newDetails = $('.content .staffList ul li:eq('+(index)+')').html();
		        $('.content .aboutStaff .staffDetails').html(newDetails);    
		               
		        $('.content .aboutStaff .staffDetails').children().css('left', '442px');        
		                                          
		        $('.content .aboutStaff .staffDetails img').animate({left: '0px'}, 800);
		        weit(50);   
		        $('.content .aboutStaff .staffDetails h3').animate({left: '0px'}, 800);     
		        weit(50);   
		        $('.content .aboutStaff .staffDetails .postion').animate({left: '0px'}, 800);   
		        weit(50);   
		        $('.content .aboutStaff .staffDetails p').animate({left: '0px'}, 800);      
        
     		}    
    	);
	}   
  	// end about us scroller
  
  	// carrier floating                     
	$('.carrierFloating').makeFloat({x:0,y:0});
  	// end carrier floating                  

  	// center algin pagination
  	if($(".pagination").length>0){
    	var contenerWidth = $(".pagination").outerWidth();                                                                                                                                                                    
              
	    var liList = $(".pagination ul li");
	    var liWidth = 0;
	    liList.each(function() {
	      	liWidth += $(this).outerWidth();     
	    });   
                 
    	$(".pagination .paginationFixPadding").css({'padding-left' : (contenerWidth-liWidth)/2}); 
	}
  	// end center algin pagination
              
	$('input[placeholder], textarea[placeholder]').placeholder();	
	
	//split menu
	$.fn.splitMenu = function() {
		this.each(function(i) {

			var list = $(this);
			
			var listWidth = list.width();
			var sumWidth = 0;
			
			
			list.children().each(function(){
				sumWidth += $(this).width();
			});

			if( listWidth < sumWidth ){
				var size = Math.ceil(list.children().length/2);
				var current_size = 0;
				list.children().each(function() {
					if (++current_size > size) {
						var new_list = $('<ul class="subMenuDown"></ul>').insertAfter(list);
						list = new_list;
						current_size = 1;
					}
					list.append(this);
				});
			}
		});
	};
	
	//end split menu
	
	$("ul#careerNavList").splitMenu();
	//$("ul#portfolioMenu").splitMenu();
	
	$('ul.subMenu li:first-child').addClass('first');
	$('ul.subMenu li:last-child').addClass('last');
	
	$('ul.subMenuDown li:first-child').addClass('first');
	$('ul.subMenuDown li:last-child').addClass('last');


	//career tabs
	$('.careerMain .subMenu li a, .careerMain .subMenuDown li a').click(function () {
		var id = $('.careerMain .subMenu li a, .careerMain .subMenuDown li a').index($(this));

		$('.careerMain .subMenu li, .careerMain .subMenuDown li').removeClass('active');

		$(this).parent().addClass('active');
		$('.careerList .post').hide().eq(id).show();
		
		return false;
	}).eq(0).click();
	//end career tabs
	
	$('.widget_anual_archive ul li a').wrapInner('<span />');
	
// validate comment form
	$('#commentform').validate({
		rules: {
			author: "required",
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			author: "Podaj imię / nazwisko",
			email: "Podaj adres e-mail",
			comment: "Podaj treść komentarza"
		}
	});
	
// validate contact form
	$('#contactform').validate({
		rules: {
			'formData[name]': "required",
			'formData[email]': {
				required: true,
				email: true
			},
			'formData[phone]': "required",
			'formData[message]': "required"
		}
	});	
	
	$('#raportform').jqTransform();
	
// validate raport form
	
	$('#raportform').validate({
		rules: {
			'formData[first_name]': "required",
			'formData[last_name]': "required",
			'formData[email]': {
				required: true,
				email: true
			},
			'formData[phone]': "required",
			'formData[company_name]': "required",
			'formData[position_in_company]': "required",
			'formData[employees_number]': "required",
			'formData[marketing_services]': "required",
			'formData[privacy_policy]': "required"
		}
	});	

	$(".input input[type=text]").hover(
	    function () {
	    	$(this).parent().find('.inputBgHover').animate({'opacity':1},300);
	    }, 
	    function () {
      		$(this).parent().find('.inputBgHover').animate({'opacity':0},300);
	    }
	);
	
	$(".input input[type=text]").focus(
	    function () {
	    	$(this).parent().find('.inputBgFocus').animate({'opacity':1},300);
	    }); 
	$(".input input[type=text]").focusout(
		function () {
      		$(this).parent().find('.inputBgFocus').animate({'opacity':0},300);
	    }
	);
	
	$(".textarea textarea").hover(
	    function () {
	    	$(this).parent().find('.textareaBgHover').animate({'opacity':1},300);
	    }, 
	    function () {
      		$(this).parent().find('.textareaBgHover').animate({'opacity':0},300);
	    }
	);
	
	$(".textarea textarea").focus(
	    function () {
	    	$(this).parent().find('.textareaBgFocus').animate({'opacity':1},300);
	    }); 
	$(".textarea textarea").focusout(
		function () {
      		$(this).parent().find('.textareaBgFocus').animate({'opacity':0},300);
	    }
	);


	$(".socilaBox li").hover(
	    function () {
	    	$(this).animate({backgroundPosition:"(37px 50%)"},300);
	    }, 
	    function () {
      		$(this).animate({backgroundPosition:"(-102px 50%)"},300);
	    }
	);

	jQuery.extend( jQuery.easing, {
		 def: 'easeInOutExpo',
	 	 easeInOutExpo: function (x, t, b, c, d) {
	  		if (t==0) return b;
	 		if (t==d) return b+c;
	    	if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
	  		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	  	}
	});

	function sizeinit() {
		var width=0,height=0;
		if( typeof( window.innerWidth ) == 'number' ) {
			width = window.innerWidth;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			width = document.documentElement.clientWidth;
		}
		if( width<1000 )
			width=1000;
		
		var url = document.location.href.split('#')[1];
		
		if(url == '' || !url)
			url = 'uslugi';	
		
		if ( url == 'uslugi') {
			height = $('#uslugi').height();
			$('.servicesPage').css('background','none');
		} else if (url == 'strategia') {
			height = $('#'+url).height();
			height = height-50;
		}else {
			height = $('#'+url).height();			
		}
		
		var offset = $('.contener').offset();
		
		$('.servicesMain').height((height)+'px');
		
		if ( url == 'uslugi') {
			$('.servicesMask').height(height+100+'px');
		} else {
			$('.servicesMask').height(height-200+'px');
		}
		$('.servicesMask').width(offset.left-50+'px');
		
		$('.serviceSection').width(width);
		$('.sectionContainer').width( $('.serviceSection').size() * width + 20);
	}

	sizeinit();

	function serviceNav(id) {
		if(id == '' || !id) id = 'uslugi';	
		
		if ( id == 'uslugi') {
			height = $('#uslugi').height();
			$('.servicesPage').css('background','none');
		} else if (id == 'strategia') {
			height = $('#'+id).height();
			height = height-50;
		} else {
			height = $('#'+id).height();			
			$('.servicesPage').css('background','');
		}
		
		var offset = $('.contener').offset();
		
		$('.servicesMain').height((height)+'px');	
		
		if ( id == 'uslugi') {
			$('.servicesMask').height(height+100+'px');
		} else {
			$('.servicesMask').height(height-200+'px');
		}
		
		$('.servicesMask').width(offset.left+250+'px');
		
		$('a.active').removeClass('active');
		$('a[href=#' + id + ']').addClass('active');
		Cufon.refresh('.servicesMenu ul li a span');
	}
	
	$.localScroll.defaults.axis = 'yx';
	
	$.localScroll.hash({
		target: '.servicesMain',
		axis: 'yx',
		lazy: true,
		easing:'easeInOutExpo',
		queue: true,
		hash: true,
		onBefore: function( e, anchor, $target ) {	
			serviceNav($(anchor).attr('id'));
		}
	});
	
	$('.servicesMenu ul, .servicesPage h1').localScroll({
		target: '.servicesMain',
		axis: 'yx',
		lazy: false,
		easing:'easeInOutExpo',
		queue: true,
		hash: true,
		onBefore: function( e, anchor, $target ) {
			serviceNav($(anchor).attr('id'));	
		}
	});
	
	if($('#map_canvas').length>0){
		initializeMap();
	}


	$('.portfolioMain .subMenu li a, .portfolioMain .subMenuDown li a').click(function () {
		var category = $(this).attr('href').substring($(this).attr('href').lastIndexOf('#') + 1);

		$('.portfolioMain .subMenu li, .portfolioMain .subMenuDown li').removeClass('active');

		$(this).parent().addClass('active');
		

		$('.portfolioMain .portfolioItem').fadeOut();
		$('.portfolioMain .portfolioItem.'+category).fadeIn();
		
		
	});

	var category = document.location.href.substring(document.location.href.lastIndexOf('#') + 1);

	$('.portfolioMain .subMenu li a[href="#'+category+'"], .portfolioMain .subMenuDown li a[href="#'+category+'"]').click();
			
	$('.mainMenu ul').slidingMenu();
	$('.servicesMenu ul').slidingMenu();
	
});


function weit(millis)
{
  var date = new Date();
  var curDate = null;
  
  do { curDate = new Date(); }
  while(curDate-date < millis);
}


function initializeMap() {
	
var map;
var soi = new google.maps.LatLng(50.086964, 19.988744);
var soiMarker = new google.maps.LatLng(50.081964, 19.993744);

	var stylez = [
		{
		  featureType: "all",
		  elementType: "all",
		  stylers: [
			{ saturation: -100 }
		  ]
		}
	];


	var mapOptions = {
		zoom: 14,
		center: soi,
		mapTypeControlOptions: {
			mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'soigray']
		},
		panControl: false,
		mapTypeControl: false,
		scaleControl: false,
  		streetViewControl: false,
  		overviewMapControl: false,
		scrollwheel: false,
		zoomControl: false,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL
		}

	};

	map = new google.maps.Map(document.getElementById("map_canvas"),
		mapOptions);
	  
	var mapType = new google.maps.StyledMapType(stylez, { name:"Grayscale" });

	var boxText = document.createElement("div");
	boxText.setAttribute("class", "infoBoxContent");
	boxText.innerHTML = "<img src='img/text/sointeractive.png'/><p><b>SoInteractive Sp. z o.o.</b></p><p>al. Jana Pawła II 41g<br />31-864 Kraków</p><p>+48 12 687 7390</p><p><a href='http://sointeractive.pl'>sointeractive.pl</a></p>";
	
	var infoWindowOptions = {
		content: boxText,
		disableAutoPan: false,
		pixelOffset: new google.maps.Size(-22, -267),
		maxWidth: 0,
		zIndex: null,
		boxStyle: { 
			opacity: 1,
			width: "180px",
			height: "220px"
		},
		closeBoxMargin: "0px",
		closeBoxURL: "img/ico/close.png",
		infoBoxClearance: new google.maps.Size(1, 1),
		isHidden: false,
		pane: "floatPane",
		enableEventPropagation: false
	};

	var infowindow = new InfoBox(infoWindowOptions);

	var marker = new google.maps.Marker({
		position: soiMarker,
		map: map,
		title:"SoInteractive"
	});

	google.maps.event.addListener(marker, "click", function (e) {
		infowindow.open(map, this);
	});
	
	infowindow.open(map,marker);
	
	map.mapTypes.set('soigray', mapType);
	map.setMapTypeId('soigray');
}




