$(function($){

var interval_id;

  $("img.change_thumbs").mouseenter(function(){
    var i=1;
      var img = $(this);

    interval_id = setInterval(function(){
      img.attr("src",img.attr("src"+i));
      i++;
      if(i>5)i=0;
    }, 500);  
    
  });

  $("img.change_thumbs").mouseout(function(){
    clearInterval(interval_id);
  });

// prepis jazykovych mutaci
var limit_watch = 8;
var counter = 0;
var timerId = 0;
var src = "";
var gallery_left = 0;
var smer = 1;
var posun_most_gallery = 0;
var total_played = 0; 
var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));

// nastavi pocet jiz shlednutych videii
if($.cookie("total_played")> 0) total_played = $.cookie("total_played");




/////////////////////////////// POP UP //////////////////////////////////////
    // pop up join access
    $(".join_access").click(function(){
            $('#over_area').show(900);   
                      window.location="/ccbill/";
   
    });

    // vypnuti prekrytu
    $('#close').click(function(){
        $('#fixed_movie').empty();
        $('#over_area').hide();
        $('#join_now').hide();
    });

/////////////////////////////// END POP UP //////////////////////////////////

/////////////////////////////// MOST POPULAR ////////////////////////////////
    // rotace galerie
    var most_popular = $("#most_popular_10");
    if( most_popular.length > 0 ){  

       posun_most_gallery = setInterval(function(){
         if(gallery_left < -1260){
            smer = 0;
          }
    
          if(gallery_left > 0){
            smer = 1;
          }
    
          if(smer == 0){
            $("#most_popular_10").css("left", (gallery_left++) + "px");
          }else{
            $("#most_popular_10").css("left", (gallery_left--) + "px");
          }
          },30);

    }

    // spusteni gallerie
    most_popular.mouseover( function(){ clearInterval( posun_most_gallery );} );
    most_popular.mouseout( 
    
    function(){
       posun_most_gallery = setInterval(function(){
         if(gallery_left < -1260){
            smer = 0;
          }
    
          if(gallery_left > 0){
            smer = 1;
          }
    
          if(smer == 0){
            $("#most_popular_10").css("left", (gallery_left++) + "px");
          }else{
            $("#most_popular_10").css("left", (gallery_left--) + "px");
          }
          },30);
  }
    
    );

/////////////////////////////// END MOST POPULAR ////////////////////////////




///////////////////////////// MAIN MODULE ///////////////////////////////////
// interval pro pretaceni shoot gallery
    if($('#main_gallery').length > 0){
        timerId = setInterval(function(){

            counter = ++counter == 5 ? 1 : counter;

            $('#movie').empty()
            $('#main_gallery #big_image').attr('src', "/modules/img/large/00" + counter + "0.jpg" );
            $('#shoot_gallery .preview').removeClass("active");
            $('#shoot_gallery .preview[position='+counter+']').addClass("active");

        }, 4000);
    }

    // po kliku spusti video
    $('#main_gallery #play').click(function(){
    		
          var so = new SWFObject("/modules/mediaplayer.swf", "sotester2", "965", "450", "9", "#000");
              so.addParam('allowfullscreen','true');
              so.addVariable('file',"./flv/00" + counter + '0.flv');
              so.addParam('width','320');
              so.addParam('height','260');
              so.addVariable('autostart','true');
              so.addParam('pluginspage','http://www.macromedia.com/go/getflashplayer');
          	  so.write("movie");
              clearInterval(posun_most_gallery);
              $('#movie').show(1500);
              clearInterval(timerId);
    });

    // po kliku na malou ikonu zmeni fotku skryje video

    $('#shoot_gallery .preview').click(function(){
        $('#movie').hide();

        counter = $(this).attr("position");
        $('#main_gallery #big_image').attr('src', "/modules/img/large/00" + counter + "0.jpg" );

        clearInterval(timerId);
        $('#movie').empty();
        $('#shoot_gallery .preview').removeClass("active");
        $(this).addClass("active");
    });
///////////////////////////// END MAIN MODULE ///////////////////////////////

///////////////////////////// PLAY MOVIE ////////////////////////////////////
    // obecne prehravani ktere se pocita na limit
    $('.play').click(function(){
        if(total_played < limit_watch){
      		var so = new SWFObject("/modules/mediaplayer.swf", "sotester", "965", "450", "9", "#000");
              so.addParam('allowfullscreen','true');
              so.addVariable('file','/trailers/' + $(this).attr("flv"));
              so.addParam('width','320');
              so.addParam('height','260');
              so.addVariable('autostart','true');
              so.addParam('pluginspage','http://www.macromedia.com/go/getflashplayer');
          	  so.write("fixed_movie");

              clearInterval(posun_most_gallery);
              total_played++;
              $.cookie('total_played', total_played, { expires: date, path: '/'});

              $('#over_area').show(900);
              $('#join_now').show();
              clearInterval(timerId);
        }else{
          alert("your daily limit was expired. You can watch "+limit_watch+" movies in one day in free tour. join with us and get unlimited access to watch!!");       
          window.location="/ccbill/";
        }
    });
///////////////////////////// END PLAY MOVIE ////////////////////////////////////

///////////////////////////// PLAY MOVIE MEMBER ////////////////////////////////////
    // obecne prehravani ktere se pocita na limit
    $('.play_member').click(function(){
      		var so = new SWFObject("/modules/mediaplayer.swf", "sotester", "965", "450", "9", "#000");
              so.addParam('allowfullscreen','true');
              so.addVariable('file','/trailers/' + $(this).attr("flv"));
              so.addParam('width','320');
              so.addParam('height','260');
              so.addVariable('autostart','true');
              so.addParam('pluginspage','http://www.macromedia.com/go/getflashplayer');
          	  so.write("fixed_movie");

              clearInterval(posun_most_gallery);
              total_played++;
              $.cookie('total_played', total_played, { expires: date, path: '/'});

              $('#over_area').show(900);
              clearInterval(timerId);
    });
///////////////////////////// END PLAY MOVIE ////////////////////////////////////


// promitani fotek news
    setInterval(function(){
      if($('#news_0 .small_image').length > 0){
           src = $('#news_0 .small_image').attr("src");
           $('#news_0 .small_image').attr("src", $('#news_0 .small_image').attr("src1"));
           $('#news_0 .small_image').attr("src1", src);
      }
    }, 2000);

    setInterval(function(){
      if($('#news_1 .small_image').length > 0){
           src = $('#news_1 .small_image').attr("src");
           $('#news_1 .small_image').attr("src", $('#news_1 .small_image').attr("src1"));
           $('#news_1 .small_image').attr("src1", src);
      }
    }, 2000);

  setTimeout(function(){
    setInterval(function(){ 
        if($('#news_0 .small_image_2').length > 0){
             src = $('#news_0 .small_image_2').attr("src");
             $('#news_0 .small_image_2').attr("src", $('#news_0 .small_image_2').attr("src1"));
             $('#news_0 .small_image_2').attr("src1", src);
        }
      }, 2000);
  }, 1000);    
  
  setTimeout(function(){ 
      setInterval(function(){
        if($('#news_1 .small_image_2').length > 0){
             src = $('#news_1 .small_image_2').attr("src");
             $('#news_1 .small_image_2').attr("src", $('#news_1 .small_image_2').attr("src1"));
             $('#news_1 .small_image_2').attr("src1", src);
        }
      }, 2000);
  }, 1000);    

////////////////////////////////// light box ///////////
  // set lightBox class
  if($('a.preview').length > 0){
    $('a.preview').lightBox();
  } 

});

//////////////////////////////// DEFINE COOKIES /////////////////////////////////////

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '; path=/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
