var runSlideShow = false;
var slideNum_ = 1;
var slideNextNum_ = slideNum_;
var slideDelay = 5000;
var body_ = null;
var timeout = null;
var checkTimeout = null;

/*Jquery*/
$("a[rel='external']").click(function(){
  var href = $(this).attr("href");
  window.open(href);
  return false;
});

$(document).ready(function(){
    
    //adservcogroup logo
    var content=$("div.content div.main-content");
    var html=content.html();
    if(html!=null) {
        html=html.replace(/(adservcogroup|adservco group)/g,"<span class=\"logo\" title=\"adservco group\"></span>");
        content.html(html);
    }

//    Cufon.replace('.main-content h2');

    body_ = $(this).find("body");
    
    body_.find("div.run-js").remove();
  
    //window & page resolution
    var wHeight = $(window).height();
    var wWidth = $(window).width();

    var dHeight = $(document).height();
    var dWidth = $(document).width();
    
    //positioning whole page
    var menuWidth = body_.find(".menu").width();
    var contentWidth = body_.find("#content .container .content").width();
    var contentHeight = body_.find("#content .container .border-limiter").height();
    
    var marginsBoth = 0;
    marginsBoth = wWidth - menuWidth;
    marginsBoth = Math.floor(marginsBoth / 2);
    body_.find("#header").width(menuWidth + marginsBoth - 115);

    marginsBoth = wWidth - contentWidth;
    marginsBoth = Math.floor(marginsBoth / 2);
    body_.find("#content .fill-left, #content .fill-right").width(marginsBoth);
    body_.find("#content .fill-left, #content .fill-right").height(contentHeight);
    
    fixContentHeight(contentHeight);
    
    
    
    //hovering
    $(".hoverable").mouseover(function(){
      var img = $(this).css("background-image");
      img = img.replace("off","hover");
      $(this).css("background-image",img);
    });
    
    $(".hoverable").mouseout(function(){
      var img = $(this).css("background-image");
      img = img.replace("hover","off");
      $(this).css("background-image",img);
    });
    
    $(".menu li a img, .slideshow .chooser img, img.hover").mouseover(function(){
      if($(".slideshow .chooser img.active, .menu li a.active img")[0] != $(this)[0]) {
        var img = $(this).attr("src");
        img = img.replace("off","hover");
        $(this).attr("src",img);
      }
    });
    
    $(".menu li a img, .slideshow .chooser img, img.hover").mouseout(function(){
      if($(".slideshow .chooser img.active, .menu li a.active img")[0] != $(this)[0]) {
        var img = $(this).attr("src");
        img = img.replace("hover","off");
        $(this).attr("src",img);
      }
    });
    
    //slideshow
    if(body_.find(".slideshow img")[0] != null) {
      body_.find(".slideshow span.loader").fadeOut("slow", function() { runSlideShow = true; timeout = window.setTimeout("toNextSlide()", slideDelay); });
    }
    
    $(".slideshow .chooser img, .slideshow .navigation span.slider").live("click", function() {
      runSlideShow = false;
      
      var that = $(this);
      var parent = that.parents(".slideshow");
      var nSlide = that.attr("alt");
      var cSlide = slideNum_;
      var img = null;
      
      slideNum_ = nSlide;
      
      parent.find(".pause").fadeIn("slow", function() { $(this).addClass("on").removeClass("off"); });
      
      img = parent.find(".chooser img.slide-" + cSlide).attr("src");
      img = img.replace("hover","off");
      parent.find(".chooser img.slide-" + cSlide).attr("src",img);
      parent.find(".chooser img.slide-" + cSlide).removeClass("active");
      
      img = parent.find(".chooser img.slide-" + nSlide).attr("src");
      img = img.replace("off","hover");
      parent.find(".chooser img.slide-" + nSlide).attr("src",img);
      parent.find(".chooser img.slide-" + nSlide).addClass("active");
      
      parent.find(".slides img.slide-" + cSlide).fadeOut("slow", function() { $(this).removeClass("active"); });
      parent.find(".navigation span.slide-" + cSlide).removeClass("active");
      parent.find(".slides img.slide-" + nSlide).fadeIn("slow", function() { $(this).addClass("active"); });
      parent.find(".navigation span.slide-" + nSlide).addClass("active");
    });
    
    $(".slideshow .navigation .pause").live("click", function() { $(this).fadeOut("slow", function() { $(this).removeClass("on").addClass("off"); runSlideShow = true; }); });
    
    //slideshow
    if(body_.find(".gallery img")[0] != null) {
      body_.find(".gallery span.loader").fadeOut("slow");
      var last = body_.find(".gallery .thumbs img:last-child").attr("alt");
      body_.find(".gallery h2 span.current").html("1");
      body_.find(".gallery h2 span.all").html(last+'');
      last -= 1;
      last *= 141;
      last += 293;
      body_.find(".gallery .thumbs .roller").width(last);
    }
    
    $(".gallery .navigation span.next").live("click", function() {
      var that = $(this);
      var parent = that.parents(".navigation");
      var img = null;
      var cPhoto = parent.find("img.active").attr("alt");
      var nPhoto = parent.find("img[alt='" + cPhoto + "']").next();
      var margin = 0;
      
      if(nPhoto[0] == null) nPhoto = parent.find("img:first-child").attr("alt");
      else nPhoto = nPhoto.attr("alt");
           
      //deactivate photo
      img = parent.find("img[alt='" + cPhoto + "']").attr("src");
      img = img.replace("_wide","_thumb");
      parent.find("img[alt='" + cPhoto + "']").attr("src",img);
      parent.find("img[alt='" + cPhoto + "']").removeClass("active").addClass("fade");
      
      //activate next photo
      img = parent.find("img[alt='" + nPhoto + "']").attr("src");
      img = img.replace("_thumb.","_wide.");
      parent.find("img[alt='" + nPhoto + "']").attr("src",img);
      parent.find("img[alt='" + nPhoto + "']").removeClass("fade").addClass("active");

      var current_path=img;
      
      //slide right
      margin = parent.find(".roller").css("margin-left");
      margin = parseInt(margin);
      
      if(nPhoto < 4) margin = 0;
      else {
        if(nPhoto > (parent.find("img:last-child").attr("alt") - 3)) margin = (parent.find("img:last-child").attr("alt") - 5) * (-141);
        else margin -= 141;
      }
      
      parent.find(".roller").animate({"margin-left": margin + "px"});
      body_.find(".gallery h2 span.current").html(nPhoto+'');
      
      body_.find(".gallery img.element").fadeOut("slow", function() {
        img = $(this).attr("src");
        //img = img.replace(cPhoto+'',nPhoto+'');
        img = current_path.replace('_wide','_display');
        
        $(this).attr("src",img);
        $(this).fadeIn("slow");
      });
    });

    $(".gallery .navigation span.previous").live("click", function() {
      var that = $(this);
      var parent = that.parents(".navigation");
      var img = null;
      var cPhoto = parent.find("img.active").attr("alt");
      var nPhoto = parent.find("img[alt='" + cPhoto + "']").prev();
      
      if(nPhoto[0] == null) nPhoto = parent.find("img:last-child").attr("alt");
      else nPhoto = nPhoto.attr("alt");
      
            
      //deactivate photo
      img = parent.find("img[alt='" + cPhoto + "']").attr("src");
      img = img.replace("_wide","_thumb");
      parent.find("img[alt='" + cPhoto + "']").attr("src",img);
      parent.find("img[alt='" + cPhoto + "']").removeClass("active").addClass("fade");
      
      //activate next photo
      img = parent.find("img[alt='" + nPhoto + "']").attr("src");
      img = img.replace("_thumb.","_wide.");
      parent.find("img[alt='" + nPhoto + "']").attr("src",img);
      parent.find("img[alt='" + nPhoto + "']").removeClass("fade").addClass("active");

      var current_path=img;
      
      //slide left
      margin = parent.find(".roller").css("margin-left");
      margin = parseInt(margin);
      
      if(nPhoto > (parent.find("img:last-child").attr("alt") - 3)) margin = (parent.find("img:last-child").attr("alt") - 5) * (-141);
      else {
        if(nPhoto < 4) margin = 0;
        else margin += 141;
      }

      
      parent.find(".roller").animate({"margin-left": margin + "px"});
      body_.find(".gallery h2 span.current").html(nPhoto+'');
      
      body_.find(".gallery img.element").fadeOut("slow", function() {
        img = $(this).attr("src");
        //img = img.replace(cPhoto+'',nPhoto+'');
        img = current_path.replace('_wide','_display');
        
        $(this).attr("src",img);
        $(this).fadeIn("slow");
      });
    });

    //iframe actions
    
    $(".main-sidebar img.map").live("click", function() {
      var iHeight = 532;
      iHeight = wHeight - iHeight;
      iHeight = Math.floor(iHeight / 2);
      body_.find("#iframe .box").css("margin-top",iHeight + "px");
      body_.find("#iframe").fadeIn("slow");
    });
    
    $("#iframe button.close").live("click", function() {
      var iframe = $(this).parents("#iframe");
      iframe.fadeOut("slow");
    });

checkTimeout = window.setTimeout("runContentFix()", 2000);

});

/*Other functions*/
function iframeCentering(wh) {
  var iHeight = $("#iframe .box").height();
      
  iHeight = wh - iHeight;
  iHeight = iHeight / 2;
  iHeight = Math.floor(iHeight);
  $("#iframe .box").css("margin-top", iHeight + "px");  
}

function fixContentHeight(ch) {
  //fixing heights
  var mcHeight = body_.find("#content .main-content").height();
  var msHeight = body_.find("#content .main-sidebar").height();
  if(msHeight < mcHeight) body_.find("#content .main-sidebar").height(mcHeight);
  else body_.find("#content .main-content").height(msHeight);
  
  if(body_.find(".main-content")[0] != null) {
    body_.find("#content .fill-left, #content .fill-right").height(ch-1);
  }
  
  if(body_.find(".gallery")[0] != null) {
    body_.find("#content .fill-left, #content .fill-right").height(ch+3);
  }
}

function runContentFix() {
    var h = body_.find("#content .container .border-limiter").height();
    fixContentHeight(h);
}

//Auto sliding
function toNextSlide() {
  if(runSlideShow){
    var img = null;
    var prevSlide = slideNum_;
    slideNextNum_ = slideNum_;
    slideNextNum_ = slideNextNum_ + 1;
    if(slideNextNum_ > 5) slideNextNum_ = 1;
    
    body_.find(".slideshow .navigation span.active").removeClass("active");
    body_.find(".slideshow .navigation span.slide-"+slideNextNum_).addClass("active");
    body_.find(".slideshow .slides img.slide-"+prevSlide).fadeOut("slow", function() { $(this).removeClass("active"); });
    body_.find(".slideshow .slides img.slide-"+slideNextNum_).fadeIn("slow"), function() { $(this).addClass("active"); };
    
    img = body_.find(".slideshow .chooser img.slide-"+prevSlide).attr("src");
    img = img.replace("hover","off");
    body_.find(".slideshow .chooser img.slide-"+prevSlide).attr("src",img);
    body_.find(".slideshow .chooser img.slide-"+prevSlide).removeClass("active");
    
    img = body_.find(".slideshow .chooser img.slide-"+slideNextNum_).attr("src");
    img = img.replace("off","hover");
    body_.find(".slideshow .chooser img.slide-"+slideNextNum_).attr("src",img);
    body_.find(".slideshow .chooser img.slide-"+slideNextNum_).addClass("active");
    
    slideNum_ = slideNextNum_;
  } 
  timeout = window.setTimeout("toNextSlide()", slideDelay);
}

