$jq(document).ready(function(){
  initFeaturedButtons();
});

function initFeaturedButtons() {
  $jq("#previousButton").click(function(){
    var index=parseInt($jq("#popup_index").attr("index"))-1;
    showIndicator();
    // open page with ajax
    popupAjax(index, false);

  })

  $jq("#nextButton").click(function(){
    var index=parseInt($jq("#popup_index").attr("index"))+1;
    showIndicator();
    // open page with ajax
    popupAjax(index, false);
  });
}


function getPageDimensions() {
  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {	
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { 
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) {
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { 
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }	

  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  if(xScroll < windowWidth){	
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  pageDimensions = { 
    height:pageHeight,width:pageWidth,scrollHeight:yScroll-windowHeight,scrollWidth:xScroll-windowWidth
  };
  return pageDimensions;	
};

function showHomePopup(obj,index) {
  $jq("#popupImage").attr("src",picture[index-1]);
  $jq(".popup_words h4").html(title[index-1]);
  $jq("#popup_index").attr("index",index);
  $jq("#previousButton").css("visibility","visible");
  $jq("#nextButton").css("visibility","visible");
  $jq("#line").css("visibility","visible");

  popupDisplay(obj);

  popupAjax(index, true);
  $jq('#popup').show();
}

function popupAjax(index, is_closed) {
  $jq.ajax({
    url: $jq.path+'index.php/'+type[index-1]+'/featured/'+masterid[index-1],
    type: 'GET',
    dataType: 'html',
    timeout: 7500,
    error: function(err) {
      $jq('#popup').hide();
      $jq('#overlay').hide();
    },
    success: function(data) {
      $jq('#indicator').hide();
      $jq('#popup').html(data);
      $jq("#popup_index").attr("index",index);
      styleHomePopup(is_closed);
      stylePopup(is_closed);
    }
  });
}

function styleHomePopup(is_closed) {
  var index = $jq('#popup_index').attr('index');
  if(index==1)
  {
    $jq("#previousButton").css("visibility","hidden");
    $jq("#line").css("visibility","hidden");
  }
  if(index>=$jq(".show_image_zone").children().length)
  {
    $jq("#nextButton").css("visibility","hidden");
    $jq("#line").css("visibility","hidden");
  }
 
  if (is_closed) {
    $('popupSpan').setStyle({
      display:'none'
    });	

    $('popup_words').setStyle({
      height:'0px',
      width:'0px',
      left: clickX + 'px',
      top: clickY + 'px'
    });
    $('popupImage').setStyle({
      height: '0px',
      width: '0px'
    });
  }
}
