$(document).ready(function(){
  

  /* BG PHOTO */

  var photoHeight = "1200";
  var photoWidth = "1200";
  var photoRatio = "1";
  var windowWidth = "0";
  var windowHeight = "0";
  var photoRatio = "0";
  var img = "1";
  var maxImg = "7";
  var marginWidth = "0";
  var ready = false;
  
	// tabs
	$('.tabs a').click(function() {
		var target = $(this).attr('href').substr(1);
		$('#'+target).show().siblings('.tab').hide();
		$(this).addClass('active').siblings().removeClass('active');
		return false;
	});
	$('.tabs a:first').click();


//  $("#bgimg img").show();

    /* load photo, resize etc. */

  $("#bgimg img").ready(function(){
    if (Get_Cookie("photoId")){
      img = Get_Cookie("photoId");
      ready = true;
    } else {
      var bgSrc = $("#bgimg img").attr("src");
      var imgxxx = bgSrc.match(/bg([0-9]+)/);
      img = imgxxx[1];
      Set_Cookie( 'photoId', img, '', '/', '', '' );
      ready = false;
    }

    resizePhoto();
  });

  $(window).resize(resizePhoto);

  function resizePhoto(){
  
/*    photoWidth = $("#bgimg img").width();
    photoHeight = $("#bgimg img").height();      
    photoRatio = photoHeight/photoWidth;
*/

    photoRatio = 1;

    windowWidth = $(window).width();
    windowHeight = $(window).height();
    windowRatio = windowHeight/windowWidth;
      
    if(windowRatio < photoRatio){
      $("#bgimg img")
        .width(windowWidth)
        .height(windowWidth*photoRatio)
        .css({left: "0px", top: ((windowHeight-$("#bgimg img").height())/2)+"px"});
    } else {
      $("#bgimg img")
        .height(windowHeight)
        .width(windowHeight/photoRatio)
        .css({top: "0px", left: ((windowWidth-$("#bgimg img").width())/2)+"px"});
    }
    
    if(ready == false){
      setTimeout(function(){
        $("#bgimg img").fadeTo(500,1);
      }, 10);
    } else {
      $("#bgimg img").show();
    }
    
    bgNaviSize();

  }




    /* next /prev */

  $("#middle").append("<a href='#prev-bg' id='prev-bg'> ‹ </a><a href='#next-bg' id='next-bg'> › </a>");
  $("#prev-bg, #next-bg").fadeTo(0,0.2);

  $("#prev-bg, #next-bg").mouseenter(function(){
    $(this).stop().fadeTo(200,1);
  }).mouseout(function(){
    $(this).stop().fadeTo(200,0.2);
  });

  $("#next-bg").click(function(){
    img++;
    changePhoto();
    return false;
  });

  $("#prev-bg").click(function(){
    img--;
    changePhoto();
    return false;
  });
    
  function changePhoto(){
  
    ready = false;

    if(img > maxImg) img = 1;
    if(img < 1) img = maxImg;
    
    Set_Cookie( 'photoId', img, '', '/', '', '' );
//    document.cookie = 'photoId=' +img;


    $("#bgimg img").fadeOut(500, function(){
      $("#bgimg").html("<img src='/templates/salonklipsi2/images/bg"+img+".jpg' alt='' />");
      $("#bgimg img").load(function(){
        resizePhoto();
      });
    });

  }

  function bgNaviSize(){
    marginWidth = $("#contentarea").offset();
    $("#prev-bg, #next-bg").width(marginWidth.left);
  }


  /* PALLOT HOVER */
  
  $(".pallot a").mouseenter(function(){
    $("img", this).stop().fadeTo(200,0.5);
  }).mouseleave(function(){
    $("img", this).stop().fadeTo(500,1);
  });
  

  /* MENU DROPDOWN */

  $("#menu li").mouseenter(function(){
    $("ul", this).slideDown(100);
  }).mouseleave(function(){
    $("ul", this).slideUp(100);
  });


  /* SET COOKIE */
  
  function Set_Cookie( name, value, expires, path, domain, secure ){
    path = "/";
    var today = new Date();
    today.setTime( today.getTime() );

    if ( expires ){ expires = expires * 1000 * 60 * 60 * 24; }
    
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
  }

  function Get_Cookie( check_name ) {
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ ){
      a_temp_cookie = a_all_cookies[i].split( '=' );
      cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

      if ( cookie_name == check_name ){
        b_cookie_found = true;
        if ( a_temp_cookie.length > 1 ){
          cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
        }
        return cookie_value;
        break;
      }
      a_temp_cookie = null;
      cookie_name = '';
    }
    if ( !b_cookie_found ){
      return null;
    }
  }

  
});  
