var site_time;
var page_time=0;
var visit_id;
var last_id=0;
var lang;
var intervalId;
var curBnr=1;

$(document).ready(function() {
  disclaimer();
  //all buttons hover
  $('button').hover(function(){ $(this).css('background-position','left bottom');}, function(){$(this).css('background-position','left top');});
  
  playBnr();
  
  $('#tabButns a').click(
    function(){
      var index = $('#tabButns a').index(this);
      if(index!=0){
        pauseBnr();
      }
      if(curBnr!=index){
        changeBnr(index);
      }
    }
  );
  
  $('#tabs li button').click(
    function(){
      pauseBnr();
      var allTabs=$('#tabsMore li');
      var index = $('#tabs li button').index(this);
      
      insPage(index+1);
      
      $(allTabs[index]).css('display','block');
      $('#tabsMore').show("slow");
      $('.butn_close').fadeIn("slow");

    }
  );
  
  $('.butn_close').click(
    function(){
      closePopUp();
      playBnr();
    }
  );
  
});
  
function closePopUp(){
  $('.butn_close').fadeOut("slow");
  $('#tabsMore').hide("slow");
  $('#tabsMore li').css('display','none');
}

function togglePopUp(index){
  $('#tabsMore li').css('display','none');
  var allPop=$('#tabsMore li');
  $(allPop[index-1]).fadeIn();
}

function pauseBnr(){
  var allButns=$('#tabButns a');
  clearInterval(intervalId);
  $(allButns[0]).addClass('butnPlay');
}

function startBnrPlay(){
  var allButns=$('#tabButns a');
  var next=curBnr+1;
  if(next==allButns.length){
    next=1;
  }
  changeBnr(next);
}

function playBnr(){
  var allButns=$('#tabButns a');
  intervalId=setInterval('startBnrPlay()',6000);
  $(allButns[0]).removeClass('butnPlay');
}

function changeBnr(index){
  var allTabs=$('#tabs li');
  var allButns=$('#tabButns a');
  
  
  if(index==0){ //play or pause
    if($(allButns[0]).hasClass('butnPlay')){
      playBnr();
    }
    else
      pauseBnr();
  }
  else{
    if (!$('#tabsMore').is(":hidden")){
      togglePopUp(index);
    }
    
    curBnr = index;
    $(allButns).removeClass('curntTab');
    $(allTabs).fadeOut("slow");
    
    $(allButns[index]).addClass('curntTab');
    $(allTabs[index-1]).show("slow");//, function(){$(this.parentNode).addClass(cls);});
  }
}

// disclaimer popup
function disclaimer(){
   $('#ftr b').click(
    function() {

      //$("#disclaimersPop").css('height','0px');
      //$("#disclaimersPop").animate({"bottom": "-550px"}, 2000);
      if ($('#disclaimersPop').is(":hidden")){
        $('#disclaimersPop').fadeIn("slow");
        $(this).addClass('down');
        insPop(0,4);
      }
      else{
        $('#disclaimersPop').fadeOut("slow");
        $(this).removeClass('down');
        
      }
      
    }
  );
   
   $('#disclaimersPop h6 em').click(
    function() {
      $('#disclaimersPop').fadeOut("slow");
        $('#ftr b').removeClass('down');
   }
   );
}

function initHit(val){
   lang = val;
   site_time= new Date().getTime();
   insSite();  
}

//ajax calls


function prepareStatisticsParams(params) {
  params.siteId = window.siteId;
  return params;
}

function insSite() {
  var parms = prepareStatisticsParams({ site: 1, lang: lang });
  
  $.ajax({  
    url: "ajaxRequest.php",    
    type: "POST",          
    data: parms,        
    cache: false,  
    success: function (html) {                
      visit_id=html;
    }         
  });   
}

function updSite(){
  var visit_time = Math.ceil((new Date().getTime()-site_time)/1000);
  new_time = new Date().getTime();
  
  if(page_time!=0)
    page_visit_time=Math.ceil((new_time-page_time)/1000);
  else
    page_visit_time=0;
  
  var parms = prepareStatisticsParams({ upd_site: 1, visit_id: visit_id, visit_time: visit_time, last_id: last_id, page_visit_time: page_visit_time });
  
  $.ajax({  
    url: "ajaxRequest.php",    
    type: "POST",          
    data: parms,        
    cache: false
  });
}


function insPage(desCont){
  new_time = new Date().getTime();
  
  if(page_time!=0)
    visit_time=Math.ceil((new_time-page_time)/1000);
  else
    visit_time=0;
    
  page_time=new_time;
  
  
  var parms = prepareStatisticsParams({page: 1, lang: lang, cont: desCont, last_id: last_id, visit_time: visit_time, visit_id: visit_id });
  
  $.ajax({  
    url: "ajaxRequest.php",    
    type: "POST",          
    data: parms,        
    cache: false,  
    success: function (html) {                
      last_id = html;
    }         
  });
}

function insPop(desCont,id){
  var parms = prepareStatisticsParams({ pop: 1, lang: lang, cont: desCont, tab: id, visit_id: visit_id });
  $.ajax({  
    url: "ajaxRequest.php",    
    type: "POST",          
    data: parms,        
    cache: false
  });
}

function GetInnerSize () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}


function ResizeToInner (w, h, x, y) {
	// make sure we have a final x/y value
	// pick one or the other windows value, not both
	if (x==undefined) x = window.screenLeft || window.screenX;
	if (y==undefined) y = window.screenTop || window.screenY;
	// for now, move the window to the top left
	// then resize to the maximum viewable dimension possible
	window.moveTo(20,20);
	window.resizeTo(screen.availWidth,screen.availHeight);
	// now that we have set the browser to it's biggest possible size
	// get the inner dimensions.  the offset is the difference.
	var inner = GetInnerSize();
	var ox = screen.availWidth-inner[0];
	var oy = screen.availHeight-inner[1];
	// now that we have an offset value, size the browser
	// and position it
	window.resizeTo(w+ox, h+oy);
}


$(document).ready(function(){
	if (typeof(supersleight) != 'undefined') {
		supersleight.init();
		supersleight.limitTo('layout');
		window.ResizeToInner(900, 670);
	} 
});

function directLoc(id){
	$('#'+id+'Link').attr('href',$('#'+id).val());
}

