function strpos (haystack, needle, offset) {
  var i = (haystack+'').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


var aCookieKeys = new Array('utm_source','utm_medium','utm_campaign');
    
if(document.referrer) {
    if(strpos(document.referrer, 'http://'+window.location.hostname+'/') === false) {

        $.cookie('http_referrer', document.referrer, { expires: 1});

        $.each(aCookieKeys, function(index, value){
            if($.getUrlVar(value)) {
                $.cookie(value, $.getUrlVar(value), { expires: 1});
            }
        })
    }
}


$(document).ready(function() {
    
    


//	if( "http://"+document.domain+"/" != document.documentURI){}	

    if($('#contentNew').length>0){
        $('#contentNew').append('<a id="zglosProblem" href="/contact/admin"><img alt="" src="/site/img/button/feedback.jpg" alt="Zgłoś problem" /></a>');
    

        $("#zglosProblem").fancybox({
            'overlayOpacity': 0.55,
            'hideOnContentClick' : false,
            'width' : 270,
            'height' : 450
        });
        
        $("#feedbackFormId").live("submit", function() {
            $('#feedbackFormId input[name=fromUrl]').val(window.location);
            $('#feedbackFormId input[name=referrerUrl]').val(document.referrer);
            
            $.ajax({
                url: $("#feedbackFormId").attr("action"),
                type: "POST",
                data: $("#feedbackFormId").serialize(),
                beforeSend: function() {
                    $.fancybox.showActivity();
                },
                success: function(data, textStatus, XMLHttpRequest) {
                    $("#fancybox-content > div").html(data);
                    $.fancybox.resize();
                },
                complete: function() {
                    $.fancybox.hideActivity();
                }
            });
            
            return false;
        });
    }	
	
	
	// ------------ load cufon
  Cufon.now();
 
	// ------------ load news fade
	if ($('#mainPageNews ul').length != 0){
		$('#mainPageNews ul').cycle({
		fx: 'fade',
		timeout: 10000,
		speed: 2000
		}); 
	}
	
	// ------------ tab box
  $(".tabAreaBox .tabHeaderRight").click(function(){
    $(this).addClass("tabActiveRight");
    $(this).parent().find(".tabHeaderLeft").removeClass("tabActiveLeft");
    $(this).parent().parent().find(".tabContentLeft").hide();
    $(this).parent().parent().find(".tabContentRight").show();
  });
  $(".tabAreaBox .tabHeaderLeft").click(function(){
    $(this).addClass("tabActiveLeft");   
    $(this).parent().find(".tabHeaderRight").removeClass("tabActiveRight");                       
    $(this).parent().parent().find(".tabContentRight").hide();
    $(this).parent().parent().find(".tabContentLeft").show();
  });
  
	// ------------ breadcrumbs sub menu
  $("#breadcrumbsNew li.subMenu").hover(
    function () {
      $(this).addClass('subMenuHover');  
  }, 
    function () {
      $(this).removeClass('subMenuHover');
  });   
   
	// ------------ lang menu
  $("#langMenu").hover(
    function () {
      $(this).find('.active').addClass('activeOver');
      $(this).find('.langList').addClass('langListOver');
  }, 
    function () {                           
      $(this).find('.active').removeClass('activeOver');
      $(this).find('.langList').removeClass('langListOver');
  });   

	// ------------ main menu 
  $("#topMenuNew .mainMenu > li").hover(
    function () {
      if ($(this).find(".sub").length != 0){
        $(this).addClass('activeOverSub');
      }
      else{
        $(this).addClass('activeOver');
      }
      
  }, 
    function () {                        
      if ($(this).find(".sub").length != 0){
        $(this).removeClass('activeOverSub');
      }
      else{
        $(this).removeClass('activeOver');
      }   
  });   
  
	// ------------ main menu 
  $("#topMenuNew .mainMenuCufon > li").hover(
    function () {
      if ($(this).find(".sub").length != 0){
        $(this).addClass('activeOverSub');
      }
      else{
        $(this).addClass('activeOver');
      }
      
  }, 
    function () {                        
      if ($(this).find(".sub").length != 0){
        $(this).removeClass('activeOverSub');
      }
      else{
        $(this).removeClass('activeOver');
      }   
  });   
    /*
  $(".subPageRight .borderBoxSmall").each(function(index) {
      i = index+1;
      if(i%2==0){       
        var heightActual = $(this).find(".borderContent").height();
        var heightPrev = $(this).prev().find(".borderContent").height();
        if(heightActual>heightPrev)
        {
          $(this).prev().find(".borderContent").css('height', heightActual+'px');
        }
        else
        {      
          $(this).find(".borderContent").css('height', heightPrev+'px');
        }
      }
    });
      */       
      
	// ------------ verictal area tab heder
  $(".subPageRight ul.vertcalArticelListTab").each(function(index) {
    $(".subPageRight ul.vertcalArticelListTab:eq("+index+") li .text").hide();  
    $(".subPageRight ul.vertcalArticelListTab:eq("+index+") li:eq(0) .text").show();  
    $(".subPageRight ul.vertcalArticelListTab:eq("+index+") li:eq(0) .text").addClass('textActive');  
    $(".subPageRight ul.vertcalArticelListTab:eq("+index+") li:eq(0) .title").hide();  
  });                                                  
  $(".subPageRight ul.vertcalArticelListTab li a.title").click(function(){
    var activeElement = $(this);  
    activeElement.parent().parent().find("div.textActive").slideUp(200, function() {
      $(this).prev().show();
      activeElement.hide();
      activeElement.next().slideDown(200, function() {   
        activeElement.parent().parent().find("div.text").removeClass('textActive');
        activeElement.next().addClass('textActive');
        equalHeight();
      }); 
    });                        
    return false;                                                  
  });
     
	// ------------ equal height border box
  equalHeight();
  function equalHeight(){
    $(".subPageRight .borderBoxSmall").each(function(index) {
      i = index+1;
      if(i%2==0){       
                                                                  
        $(this).find(".borderContent").height('auto');
        $(this).prev().find(".borderContent").height('auto');  
        var heightActual = $(this).find(".borderContent").height();          
        var heightPrev = $(this).prev().find(".borderContent").height();
        if(heightActual>heightPrev){
          $(this).prev().find(".borderContent").css('height', heightActual+'px');
        }else{      
          $(this).find(".borderContent").css('height', heightPrev+'px');
        }
      }
    });
  }
  
  // ------------ press center - social page
  if($("#contentNew .socialPortalPressCenter ul").length>0){                                     
    var socialPortalPressCenter = 0;
    $("#contentNew .socialPortalPressCenter ul").each(function(index) {
      widthTemp = 0;
      $($(this).find('li')).each(function(index) { 
        widthTemp = widthTemp + $(this).innerWidth();
      });
      if(widthTemp>socialPortalPressCenter){socialPortalPressCenter = widthTemp};
    });
    $("#contentNew .socialPortalPressCenter").css('width', socialPortalPressCenter+'px');
  }
  
  // ------------ industries page
  if($("#contentNew .subPageIndustries .borderBox").length>0){                                     
    var borderBoxIndustriesHeightP = 0;                                     
    var borderBoxIndustriesHeightUL = 0;
    $("#contentNew .subPageIndustries .borderBox").each(function(index) {
      
      borderBoxIndustriesHeightPtemp =  $($(this).find('p:eq(0)')).height();
      if(borderBoxIndustriesHeightPtemp>borderBoxIndustriesHeightP){borderBoxIndustriesHeightP = borderBoxIndustriesHeightPtemp};
      
      borderBoxIndustriesHeightULtemp =  $($(this).find('ul')).height();                                                             
      if(borderBoxIndustriesHeightULtemp>borderBoxIndustriesHeightUL){borderBoxIndustriesHeightUL = borderBoxIndustriesHeightULtemp};
    });
    $("#contentNew .subPageIndustries .borderBox .wysiwyg p:first-child").css('height', borderBoxIndustriesHeightP+'px');
    $("#contentNew .subPageIndustries .borderBox ul").css('height', borderBoxIndustriesHeightUL+'px');
  }

        if($("#contentNew .subPageIndustries .borderBox").length>0){                                     
    
            var iBorderBoxHeight = 0;
            var iBorderBoxHeightH2 = 0;
            $("#contentNew .subPageIndustries .borderBoxTop").each(function(index) {
                if($($(this).find('p:eq(0)')).height() > iBorderBoxHeight) {
                    iBorderBoxHeight = $($(this).find('p:eq(0)')).height();
                }
                if($(this).find('h2').height() > iBorderBoxHeightH2) {
                   iBorderBoxHeightH2 = $(this).find('h2').height();
                }
            })
            $("#contentNew .subPageIndustries .borderBoxTop .wysiwyg p:first-child").css('height', iBorderBoxHeight+'px');
            $("#contentNew .subPageIndustries .borderBoxTop h2").css('height', iBorderBoxHeightH2+'px');
        }
        
	// ------------ search result list 
  $(".searchResultList .searchResultItemText").hide();       
  $(".searchResultList .searchResultItem").last().addClass('searchResultItemOpen');
  $(".searchResultList .searchResultItemText").last().show();                                
  $(".searchResultList .searchResultItemHead").click(function(){   
    var activeElement = $(this);  
    
    if(activeElement.parent().hasClass('searchResultItemOpen')){ 
      activeElement.parent().removeClass('searchResultItemOpen');
      activeElement.next().slideUp(200, function() {});    
    }
    else{                  
      activeElement.parent().addClass('searchResultItemOpen');
      activeElement.next().slideDown(200, function() {});  
    }                                                              
  });
               
 
	if ($('.subPageIndustries').length>0){
		$('.subPageIndustries .borderBox:last').addClass('borderBoxLast'); 
	}
	
	if ($('.sectorPageIndustriesBottomBox').length>0){
		$('.sectorPageIndustriesBottomBox .verticalBox:last').addClass('verticalBoxLast'); 
	}
      
      
  $("#contentNew .quartersListWord ul").each(function(index) {
    height = 0;
    $(this).find('li').each(function(index) {
      newHeight = $(this).height();
      if(newHeight>height) {height = newHeight}; 
    });
    $(this).find('li').css('height', height);
  });              
      
});

                        
Cufon.replace('#top #topMenuNew ul.mainMenuCufon li a.main', {fontFamily: 'Breuer Text',textShadow: '1px 1px #00446e', hover: true, hoverables:{a:true}});
Cufon.replace('#top .topBannerHeadText', {fontFamily: 'Breuer Text'});
                                    
Cufon.replace('h1, strong.titleBox, h2.titleBox, #contentNew .tabArea .tabHeader, #contentNew .subPageLeftEqualLong h2', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});
                                    

Cufon.replace('#contentNew .borderMenuBox ul.menuLinkList li a', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});

Cufon.replace('#contentNew form ul li h3, #contentNew .applyForm h3', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});
Cufon.replace('#contentNew .tabMenu ul li a, #contentNew .tabContentMenu ul li a', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});

Cufon.replace('#contentNew .tvUseraArea a.account', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});
Cufon.replace('#contentNew .quartersListWord h2', {fontFamily: 'Breuer Text Light', hover: true, hoverables:{a:true}});

                    
