/* General Setting - Dynamic Height 
        - Every div with the class fillout, will fillout
*/
jQuery.noConflict();
var browser         = jQuery.browser;
var browserVersion  = jQuery.browser.version;
var previousColumn  = "";
var ColumnCounter   = 0;
var bToggle         = false;
var toggleHeight    = 55;
var bAjaxLoad       = false;
var PreviousItem    = "";
var allTogether     = 0;
var minHeight       = 0;
var bIsSet          = false;

jQuery(document).ready(function() {     
    Custom.init();

    jQuery("a[rel^='lightbox']").prettyPhoto();
           
    jQuery(window).resize(function(){
      resizeHandler();
    });
    /* Set pretty checkboxes */
    jQuery("input[type=checkbox]").each(
        function(){
            jQuery(this).addClass("styled");
        }
    );
    jQuery("input[type=radio]").each(
        function(){
            jQuery(this).addClass("styled");
        }
    );
    /*jQuery("select").each(
        function(){
            jQuery(this).addClass("styled");
        }
    ); */
    
    if(jQuery('img.tooltip-handler').length > 0){
      jQuery('img.tooltip-handler').tooltip({
        delay: 0,
        showURL: false,
        bodyHandler: function() {
          return jQuery(this).parent().find('.tip-text').html();
        }
      });
    }
    
    
    jQuery('.divFoldGreen .detailHead').each(function(){
      var _this = jQuery(this);
      _this.click(function(){
        resumeFold(_this);
      });
    });
    
    if(jQuery('ul#slidepictures li').length > 0){ 
        jQuery('ul#slidepictures').css('margin-left', '0px');
        jQuery('ul#slidepictures').jcarousel();
    }
    
    if(jQuery('ul#slidevideos li').length > 0){
        jQuery('ul#slidevideos').css('margin-left', '0px');
        jQuery('ul#slidevideos').jcarousel();
    }
    
    if(jQuery('ul#slideslideshows li').length > 0){ 
        jQuery('ul#slideslideshows').css('margin-left', '0px');
        jQuery('ul#slideslideshows').jcarousel();
    }
    
    if(jQuery('.vacancy-overview').length > 0 || jQuery('.company-overview').length > 0){
        if(typeof(GoogleMap) != 'undefined') {
          GoogleMap.HideAllMarkers();
        }

        jQuery('a.map_marker').each(function(){
            var markerId = jQuery(this).attr('id');
            var markerId = markerId.toString().replace("map_","");
            var markerId = markerId.toString().replace("employer_","");
            GoogleMap.ShowMarker(markerId);
        });   
    }
       
    jQuery(window).trigger('resize');
    
   jQuery('.fileObject').si_files({
      button: {
        src: '/pics/btn/btn-browse.png',
        width: 70,
        height: 20
      }
    });
    
    jQuery('a.GetBlogPost').click(function(){
      return GetBlogPost(jQuery(this));
    });
    
    
    if(jQuery('a.SwitchArchive').length > 0){
      SwitchArchiveInit('a.SwitchArchive');
      jQuery('a.SwitchArchive').click(function(){
        return SwitchArchive(jQuery(this));
      });
    }
});

function centerImage(){
  if(jQuery('.company-logo').length > 0){
        var top = ( jQuery('.company-logo').height() - jQuery(".company-logo img").height() ) / 2; 
        jQuery(".company-logo img").css('margin-top', top);    
    }
}

function ToggleDiv(CheckboxElement, DivName){
    if(jQuery(DivName).css('display') == 'none') {
       jQuery(DivName).css('display','block');

    } else {
       jQuery(DivName).css('display','none');

    }
}

function ClearFile(Element){
  var ClickedElement = jQuery(Element);
  var PrevFileElement = ClickedElement.prev().find(':file');
  var ElementName = PrevFileElement.attr('name');
  var ElementClass = PrevFileElement.attr('class');
  ClickedElement.prev().remove();
  ClickedElement.parent().prepend('<input class="' + ElementClass + '" type="file" name="' + ElementName + '" style="position: relative; width: auto; opacity: 0; cursor: pointer; left: 0px; top: 0px;" />');
  jQuery('.fileObject').si_files({
      button: {
        src: '/pics/btn/btn-browse.png',
        width: 70,
        height: 20
      }
    });
}

/* Dynamic Height - See top for settings!*/
function callVacancy(Id, element){
    if(!bAjaxLoad){
            bAjaxLoad = true;
            var _this = element;
            if(jQuery(_this).length > 0){
                var url = jQuery(_this).attr("href");
            } else {
                var url = _this;
            }
            
            jQuery("#mapsButton").removeClass("active");
            jQuery("#vacancyButton").removeClass("disabled");
            jQuery("#vacancyButton").addClass("active");
            jQuery("img.legenda").hide();
            var ContentWindow = jQuery("#contentWindow");
            ContentWindow.css({"display":"block", "z-index":"100"});            
            ContentWindow.html('<img src="http://www.vividlinks.eu/pics/loader.gif" style="margin-left: 163px; margin-top: 131px;" />');
            if(jQuery("#contentWindow").css('display') == "block"){
                if(PreviousItem != ""){
                    jQuery(PreviousItem).removeClass("active");
                }
            }
        
            jQuery("#contentWindow").load(url + "?callback=1&vid=" + Id, function(){
                    Cufon.replace('h1');    
                    Cufon.replace('h4');
                    Cufon.replace('#content .tagcloud', {hover: true});
                    jQuery(_this).addClass("active");
                    PreviousItem = jQuery(_this);
                    bAjaxLoad = false;
                    resizeHandler();
            });

            return false;
        } else {
            return false;
        }
}

function callEmployer(Id, element){
    if(!bAjaxLoad){
        bAjaxLoad = true;
        var _this = element;
        if(jQuery(_this).length > 0){
            var url = jQuery(_this).attr("href");
        } else {
            var url = _this;
        }
        jQuery("#mapsButton").removeClass("active");
        jQuery("#vacancyButton").removeClass("disabled");
        jQuery("#vacancyButton").addClass("active");
        jQuery("img.legenda").hide();
        jQuery("#contentWindow").css("display","block");
        jQuery("#contentWindow").css("z-index","100");
        jQuery("#contentWindow").html('<img src="http://www.vividlinks.eu/pics/loader.gif" style="margin-left: 163px; margin-top: 131px;" />');
        
        jQuery("#contentWindow").load(url + "?callback=1&eid=" + Id, function(response){   
          Cufon.replace('h1');    
          Cufon.replace('h4');
          Cufon.replace('.detailHead');
          Cufon.replace('#content .tagcloud');       
          jQuery('.divFold .detailHead').each(function(){
            var _this = jQuery(this);
            _this.click(function(){
              companyFold(_this);
            });
          });
          
          jQuery('.companyDescription').find('.detailHead').click();
          jQuery("#contentWindow #ajaxCall div").removeClass("scroll-container");
          jQuery("#contentWindow #ajaxCall #scroller").addClass("scroll-container");

          bAjaxLoad = false;
          resizeHandler();
        });   
        return false;
    } else {
        return false;
    }
}

function showMaps(){
    jQuery("#vacancyButton").removeClass("active");
    jQuery("#contentWindow").hide();
    jQuery("#contentWindow").css('z-index', '-100');
    jQuery("img.legenda").show();
    jQuery("#mapsButton").addClass("active");
    return false;
}

function showVacancy(){
    if(jQuery("#vacancyButton").hasClass("disabled")){
        return false;    
    }
    jQuery("#mapsButton").removeClass("active");
    jQuery("#contentWindow").show();
    jQuery("#contentWindow").css('z-index','10');
    jQuery("img.legenda").hide();
    jQuery("#vacancyButton").addClass("active");
    return false;
}
      
function resizeHandler(){    
  if(jQuery("#searchForm").length > 0){
    jQuery('.scroll-container').each(function(){
      if(!jQuery(this).hasClass('medium')){
        if(!jQuery(this).hasClass('medium-large-large')){
          jQuery(this).jScrollPaneRemove();
        }
      }
    });
   
    var Height    = parseInt(jQuery(".SerachForm")[0].scrollHeight);
     var PaddingSize = 35;
    jQuery('.column.width_795.fillout .text').css('height',Height + 'px');
    jQuery('.inside-column198 .scroll-container.medium').css('height', (Height - PaddingSize) + 'px');
    jQuery('.inside-column198 .scroll-container.medium-large-large').css('height', ((Height - PaddingSize) - 32) + 'px'); /* minus 32 because of links on top */
    jQuery('.inside-column360 .scroll-container.special').css('height', ((Height - PaddingSize)) + 'px'); /* minus 32 because of links on top */
    jQuery('#ajaxCall .scroll-container').css('height', ((Height - PaddingSize)) + 'px'); /* minus 32 because of links on top */
    
    jQuery('#contentWindow').css('height', (Height - 20) + 'px'); /* minus 2 because of border sizes */
    jQuery('.inside-column400 .scroll-container.middle').css('height', ((Height - PaddingSize) - 35) + 'px'); /* minus 35 because of top padding */
    jQuery('.scroll-container').each(function(){
      if(!jQuery(this).hasClass('medium') || !jQuery(this).parent().hasClass('jScrollPaneContainer')){
        if(!jQuery(this).hasClass('medium-large-large') || !jQuery(this).parent().hasClass('jScrollPaneContainer')){
          jQuery(this).jScrollPane({scrollbarWidth: 10});
        }
      }
    });
    
  }
}

function searchCallbackCompanies(){
    var str = jQuery("#searchForm").serialize();
    if(jQuery("#contentWindow").css('display') == "none"){
      jQuery("#contentWindow").css({'z-index':'-100', 'display':'block'});
    }
    jQuery('.inside-column198:eq(1) .scroll-container').html('<img style="margin-left: 71px;" src="http://www.vividlinks.eu/pics/loader.gif" />');
    jQuery('.inside-column198:eq(1)').load(jQuery("#searchForm").attr("action"), str,function(){  
      jQuery("#contentWindow").css('z-index','10');

        jQuery(window).trigger('resize'); 
        Cufon.replace('h1');
        jQuery("#vacancyButton").addClass("disabled");
        showMaps();
        GoogleMap.HideAllMarkers();

        jQuery('a.map_marker').each(function(){
            var markerId = jQuery(this).attr('id');
            var markerId = markerId.toString().replace("employer_","");
            GoogleMap.ShowMarker(markerId);
        });
    });    
    return false;
}

function searchCallbackVacancies(){
    var str = jQuery("#searchForm").serialize();
    
    var TotalVacancyCount = jQuery('#totalvacancycount').html();
    jQuery('.inside-column198:eq(1) .scroll-container').html('<img style="margin-left: 71px;" src="http://www.vividlinks.eu/pics/loader.gif" />');
    jQuery('.inside-column198:eq(1)').load(jQuery("#searchForm").attr("action") + ' .inside-column198:eq(1)', str,function(){         
      jQuery('#TotalVacancyItems').html(TotalVacancyCount);  
      jQuery(window).trigger('resize'); 
        Cufon.replace('h1');
        jQuery("#vacancyButton").addClass("disabled");
        showMaps();
        GoogleMap.HideAllMarkers();

        jQuery('a.map_marker').each(function(){
            var markerId = jQuery(this).attr('id');
            var markerId = markerId.toString().replace("map_","");
            GoogleMap.ShowMarker(markerId);
        });
        
    });  
    return false;
}

function resetSearch(vacancySearch){
    if(vacancySearch){
        divElement = " .inside-column198:eq(1)";
    } else {
        divElement = "";
    }
    jQuery("#searchForm input:text").attr('value','');
    jQuery("#searchForm input:checkbox").attr('checked','checked');
    jQuery("#searchForm input").removeAttr('selected');

    jQuery('.inside-column198:eq(1) .scroll-container').html('<img style="margin-left: 71px;" src="http://www.vividlinks.eu/pics/loader.gif" />');
    
    
    GoogleMap.ShowAllMarkers();
    jQuery('.inside-column198:eq(1)').load(jQuery("#searchForm").attr("action") + divElement,'reset=1&stripped=1',function(){        
        jQuery(window).trigger('resize'); 
        Cufon.replace('h1');
        jQuery("#vacancyButton").addClass("disabled");
        showMaps();
        GoogleMap.HideAllMarkers();

        jQuery('a.map_marker').each(function(){
            var markerId = jQuery(this).attr('id');
            var markerId = markerId.toString().replace("map_","");
            var markerId = markerId.toString().replace("employer_","");
            GoogleMap.ShowMarker(markerId);
        }); 
    });
    Custom.clear();   
    return false;
}

function windowHide(){
    if(jQuery("#contentWindow").css('right') == "0px"){
        jQuery("#contentWindow").animate({
            right: "-300px"
        },1500);
        return false;
    }
    return false;    
}

function toggleWidget(element, portal) {    
   var jqueryElement = jQuery(element).parent().parent();
   var backgroundUrl = jqueryElement.find('.widget_w .widget_content').css('background-image');
             
   if(jqueryElement.find('.widget_w .widget_content').css('height') == toggleHeight + "px"){
        if(backgroundUrl != "none"){
            jqueryElement.find('.widget_w .widget_content').css('background','#ffffff none');
            jqueryElement.find('.widget_sw').css('background','url(pics/bottom.png) top left no-repeat');
        }
        jqueryElement.find('.widget_w .widget_content').animate({height: jqueryElement.find('.widget_w .widget_content')[0].scrollHeight + "px"},function(){
            if(backgroundUrl != "none"){
                jQuery(this).css('background','#ffffff url(pics/home-bg.gif) bottom left repeat-x'); 
                jqueryElement.find('.widget_sw').css('background','url(pics/bottom-green.png) top left no-repeat');
            }
        });
        jQuery(element).removeClass("toggle-widget-active");
        jQuery(element).addClass("toggle-widget");
   } else {
        var height = jqueryElement.find('.widget_w .widget_content').innerHeight();
        if(height > toggleHeight){
            if(backgroundUrl != "none"){
                jqueryElement.find('.widget_sw').css('background','url(pics/bottom.png) top left no-repeat');
                jqueryElement.find('.widget_w .widget_content').css('background','#ffffff none');
            }
            jqueryElement.find('.widget_w .widget_content').animate({height: toggleHeight + "px"},function(){
                if(backgroundUrl != "none"){
                    jQuery(this).css('background','#ffffff url(pics/home-bg.gif) 0px 5px repeat-x');
                    jqueryElement.find('.widget_sw').css('background','url(pics/bottom-green.png) top left no-repeat');
                }
                
            });
            jQuery(element).removeClass("toggle-widget");
            jQuery(element).addClass("toggle-widget-active");
        }
   }
   setTimeout(function(){bToggle = true; onUpdate(portal);}, 500);
 }
 
function onUpdate(portal, widget) {
    var totalItems      = 0;
    var PlaceCounter    = 0;
    var previousColumn  = -1;
    var itemFolded      = 0;
    
    if(!bToggle){
        jQuery(".widget").each(function(){
            if (totalItems % 2 == 0){
                if(jQuery(this).find('.widget_content').css('height') == toggleHeight + 'px'){
                    jQuery(this).find('.widget_content').css('background','#ffffff url(pics/home-bg.gif) 0px 5px repeat-x');
                    jQuery(this).find('.widget_sw').css('background','url(pics/bottom-green.png) top left no-repeat');
                } else {
                    jQuery(this).find('.widget_content').css('background','#ffffff url(pics/home-bg.gif) bottom left repeat-x');
                    jQuery(this).find('.widget_sw').css('background','url(pics/bottom-green.png) top left no-repeat');    
                }
            } else {
                jQuery(this).find('.widget_content').css('background','#ffffff none');
                jQuery(this).find('.widget_sw').css('background','url(pics/bottom.png) top left no-repeat'); 
            }
            totalItems++;
        });
    } 

    var aSplittedValues = portal.serializeAdd().split(",");
    aSplittedValues.pop();
    for( var i = 0; i < aSplittedValues.length; i++){
        
        var aFinalValues = aSplittedValues[i].split("-");
        if(aFinalValues[0].slice(-1) == previousColumn){
            PlaceCounter++;
        } else {
            PlaceCounter = 0;
        }
        if(jQuery("#" + aFinalValues[1]).find('.widget_content').css("height") == "55px"){
            itemFolded = 1;
        } else {
            itemFolded = 0;    
        }
        setCookie(aFinalValues[1], aFinalValues[0].slice(-1) + "-" + PlaceCounter + "-" + aFinalValues[1].slice(-1) + "-" + itemFolded, 365)
        
        previousColumn = aFinalValues[0].slice(-1);
    }
    bToggle = false;   
}

 function clearTxt(element)
{
    if (element.value == "..."){
        element.value = "";
    }
}
function checkTxt(element)
{
    if (element.value == ""){
        element.value = "...";
    }
}

function toggleSearch(){
    if(jQuery('#advancedSearch').css('height') == "0px"){
        jQuery('#advancedSearch').animate({height: "112px"}, "slow");
        return false;
    } else {
        jQuery('#advancedSearch').animate({height: "0px"}, "slow");
        return false;    
    }
}

function companyFold(DivElement){
  var ParentDiv   = DivElement.parent();
  var SpanElement = DivElement.find('span');

  ParentDiv.css('overflow', 'hidden');
  if(ParentDiv.css('height') != '10px'){
    ParentDiv.animate({'height':'10px'}, function(){
      jQuery('.scroll-container .special').jScrollPaneRemove();
      jQuery('#scroller').jScrollPaneRemove();      
      jQuery('.scroll-container .special').jScrollPane({scrollbarWidth: 10});
      jQuery('#scroller').jScrollPane({scrollbarWidth: 10});
    });
    SpanElement.css('background','url(pics/bg_div_fold_arrowDown.gif) top left no-repeat');
  } else {
    ParentDiv.animate({'height': ParentDiv[0].scrollHeight + 'px'}, function(){
      jQuery('.scroll-container .special').jScrollPaneRemove();
      jQuery('#scroller').jScrollPaneRemove();
      jQuery('.scroll-container .special').jScrollPane({scrollbarWidth: 10});
      jQuery('#scroller').jScrollPane({scrollbarWidth: 10});
    });
    SpanElement.css('background','url(pics/bg_div_fold_arrowUp.gif) top left no-repeat');
  }
  
}

function resumeFold(DivElement){
  var ParentDiv   = DivElement.parent();
  var SpanElement = DivElement.find('span');
  var bAllreadyFolding    = false;
  
  ParentDiv.css('overflow', 'hidden');
  if(!bAllreadyFolding){
    bAllreadyFolding = true;
    if(ParentDiv.css('height') != '25px'){
      ParentDiv.animate({'height':'25px'});
      SpanElement.css('background','url(/pics/arrow-down.gif) top left no-repeat');
      jQuery('a.resumeStep').each(function(){
        if(jQuery(this).attr('rel') == jQuery('a',DivElement).attr('name')) {
          jQuery(this).removeClass('active');
        }
      }); 
    } else {
      ParentDiv.animate({'height': ParentDiv[0].scrollHeight + 'px'});
      SpanElement.css('background','url(/pics/arrow-up.gif) top left no-repeat');
      jQuery('a.resumeStep').each(function(){
        if(jQuery(this).attr('rel') == jQuery('a',DivElement).attr('name')) {
          jQuery(this).addClass('active');
        }
      });
    }
  }
  
  /*else {
    jQuery('.divFoldGreen').each(function(){
      if(jQuery(this).css('height') != '25px'){
        jQuery(this).animate({'height':'25px'},function(){
          jQuery(this).find('.foldArrow').css('background','url(/pics/arrow-down.gif) top left no-repeat');
          ParentDiv.animate({'height': ParentDiv[0].scrollHeight + 'px'});
          SpanElement.css('background','url(/pics/arrow-up.gif) top left no-repeat');
        });
        bAllreadyFolding = true;
      }
    });*/
}

function CheckResumeDiv(){
  Hash = window.location.hash;
  Hash = Hash.replace("#","");

  jQuery('a.foldDiv').each (function(){
    if(jQuery(this).attr('name') == Hash){
      resumeFold(jQuery(this).parent());  
    }
  });
}

function GetBlogPost(ClickedElement){
  if(!bAjaxLoad){
    var url = ClickedElement.attr('href');
    jQuery(".blog-column.big").html('<img src="http://www.vividlinks.eu/pics/loader.gif" />');
    jQuery(".blog-column.big").load(url + " #blog-info", function(){
                      jQuery('#blog-info').css('text-align','left');
                      jQuery('#ListOptions').css('display','block');
                      Cufon.replace('h1');
                      Cufon.replace('h5');
                      Cufon.replace('.tagcloud a');
                      Cufon.replace('a.edit');
                      ClickedElement.addClass("active");
                      
                      jQuery('.message-box').each(function(){
                        jQuery('a', jQuery(this)).each(function(){
                          var URL = jQuery(this).attr('href');
                          if(URL.search('youtube.com') != -1){
                            jQuery(this).wrap('<div class="youtube" />');
                            FetchYouTubeFileForBlog(URL,jQuery(this).parent());
                          }
                        });
                      });
                      
                      
                      bAjaxLoad = false;
                      resizeHandler();
              });
  } 
  return false;
   
}
var bAnimation = false;

function SwitchArchiveInit(ElementClass){
  jQuery(ElementClass).each(function(){
    if(jQuery(this).hasClass('closed')){
      jQuery(this).next('ul').css('display','none');
    }
  });
}

function SwitchArchive(ClickedElement){
  if(bAnimation){
    return false;
  }
  bAnimation = true;
   
  ClickedElement.next('ul').slideToggle("slow",function(){
    if(ClickedElement.hasClass('opened')){
      ClickedElement.removeClass('opened');
      ClickedElement.addClass('closed');
    } else {
      ClickedElement.removeClass('closed');
      ClickedElement.addClass('opened');
    }  
    bAnimation = false;
  });
   
  return false;  
}

function FetchYouTubeFileForBlog(YouTubeUrl, DivForYouTube){
  if(YouTubeUrl != ''){
    var VideoID;
    var UrlCheckResult;

    UrlCheckResult = YouTubeUrl.match("[\\?&]v=([^&#]*)");

    VideoID = ( UrlCheckResult === null ) ? YouTubeUrl : UrlCheckResult[1];
    var CorrectUrl = "http://www.youtube.com/v/"+VideoID+""; 
  
    if(VideoID.search('youtube.com') == -1){
      DivForYouTube.html('<object width="270" height="203"> \
                            <param name="movie" value="' + CorrectUrl + '"></param> \
                            <param name="wmode" value="transparent"></param> \
                            <embed src="' + CorrectUrl + '" type="application/x-shockwave-flash" wmode="transparent" width="270" height="203"></embed> \
                          </object> \
                          ');
    }
                                
  }
}
