//LIGHTBOX
$(document).ready(function(){
   $(".photo_gallery a:first").click(function(event){
      $(this).next("a").click();      
   });
   $(".photo_gallery a").not(":first").lightBox();
});

//INITIAL LAYOUT SETUP
$(document).ready(function(){
    $("#layer_02 > div").each(function(){
        $("#layer_00").append('<div id="bkg_0'+($(this).prevAll("div").length+1)+'"></div>');
    });
    setupLayout();
    $(window).resize(function(){
        $("#navigation li").removeClass("active");
        setupLayout();
    });
    
    function setupLayout(){
        $("html,body").animate({scrollLeft: "0px"},500);
        var movementX = ($(window).width()-970)/2;
        var movementY = ($(window).height()-590)/2;
        if(movementY<0){ movementY=0; }
        var screenWidth = $(window).width();
        var elWidth = (970*$("#layer_02 > div").length)+((screenWidth*2)*$("#layer_02 > div").length)+movementX;
        $("#logos,#homelink").css({left:10+movementX+"px",top:2+movementY+"px"});
        
        
        $("#layer_00").css({width: elWidth+movementX+"px"});
        $("#layer_01").css({width: elWidth+movementX+"px",paddingTop:movementY+"px"});
        $("#layer_02").css({paddingLeft:movementX+"px",width: elWidth+"px"});
        $("#layer_02 > div").not(":last").css({marginRight:(screenWidth*2)+"px"});
        $("#layer_02 > div").each(function(){
              var el = $("#bkg_0"+($(this).prevAll("div").length+1));
              var offset = $(this).offset();              
              el.css({left:offset.left-200,top:offset.top});
        });
    }
});

//SCROLL SITE
$(document).ready(function(){
    $("#homelink a.fc").click(function(){
        $("html,body").stop();
        $("#navigation li").removeClass("active");
        $("html,body").animate({scrollLeft: 0},1250,"easeOutBack");
    });
    $(".jump_to_rules").click(function(){
        $("#navigation li:last a").click();
    });    
    $("#navigation a").click(function(){
        if(typeof playerPopupVideo!="undefined"){
          playerPopupVideo.sendEvent("STOP");
        }
        $("html,body").stop();
        $(this).closest("li").addClass("active").siblings("li").removeClass("active");
        var offset = $("#page_0"+($(this).closest("li").prevAll("li").length+2)).offset();
        $("html,body").animate({scrollLeft: offset.left-parseFloat($("#layer_02").css("padding-left"))},1250,"easeOutBack");
    });
});

//SCROLL REPLACEMENT
$(document).ready(function() {
    $("#page_02 .scrolling_box").jScrollPane();
    $("#page_05 .navigation_block div").jScrollPane();
    $("#page_05 #box_type01 .text").jScrollPane();
    $("#box_type02 .scroll").jScrollPane();
    $("#fallback_page .text .scroll").jScrollPane();
    
});

//QUIZ
$(document).ready(function(){
    if($("#quiz .timer").html()!=null){ initTimer(); }

 
    $("#page_02 #login").live("submit",function(){
        var data = $(this).serializeArray();                               
        $.post($(this).attr("action").replace("index.php","simple.php"), data, function(data){                       
             $("#page_02 .text,#page_02 form").remove();
             $("#page_02").append(data);
             $("#box_type02 .scroll").jScrollPane();                   
        },"html");
        return false;
    });
    
    $("#page_02 #special_button").live("submit",function(){
        var data = $(this).serializeArray();                               
        $.post($(this).attr("action").replace("index.php","simple.php"), data, function(data){                       
             $("#box_type02,#special_button").remove();
             $("#page_02").append(data);
             initTimer();             
             $("#box_type02 .scroll").jScrollPane(); 
        },"html");
        return false;
    });
    
    $("#page_02 #quiz ul a").live("click",function(){
        $.post($(this).attr("href").replace("index.php","simple.php"), function(data){
            $("#page_02").empty();
            $("#page_02").append(data);
            $("#box_type02 .scroll").jScrollPane();
            initTimer();
        });
        return false;
    });
    
    $("#page_02 .forgotten_pass,#page_02 .register,#page_02 .back_button,#page_02 .lnk_highscores,#page_02 .lnk_rules").live("click",function(){
        $.post($(this).attr("href").replace("index.php","simple.php"), function(data){
            $("#page_02").empty();
            $("#page_02").append(data);
            $("#box_type02 .scroll").jScrollPane();
            
        });        
        return false;
    });
    
    $("#page_02 #deafult_form").live("submit",function(){
        var data = $(this).serializeArray();                               
        $.post($(this).attr("action").replace("index.php","simple.php"), data, function(data){                       
             $("#page_02").empty();
             $("#page_02").append(data);
             $("#box_type02 .scroll").jScrollPane();
        },"html");
        return false;
    });
    
});

var timerInterval=false;
var newTime;
function initTimer(){
  if($("#quiz .timer").html()!=null){
      var time = $("#quiz .timer .clock").text().split(":");      
      var min = parseInt(time[0])*60;
      var sec = parseInt(time[1]);
      if(min==0){
        min = parseInt(time[0].substr(1))*60;
      }
      if(sec==0){
        sec = parseInt(time[1].substr(1));
      }
      
      newTime = min + sec;
      
      if(timerInterval) { clearInterval(timerInterval); }
      timerInterval = setInterval("startTimer()",1000);       
  }
}

function startTimer(){
    newTime = newTime-1;
    var min = parseInt(newTime/60);
    var sec = parseInt(newTime%60);
    if(min==0 && sec==0){ clearInterval(timerInterval); }
    if (min<10){ min="0"+min; }
    if (sec<10){ sec="0"+sec; }    
    $("#quiz .timer .clock").text(min+":"+sec);
}


//DOWNLOAD DROPDOWN & TAB SWITCHER
$(document).ready(function(){   
    $("#page_03 .block div").each(function(){
        if( $(this).closest("dl").is(".type_01") ){
            $(this).find("a").addClass("drop-title");
        } else if ( $(this).closest("dl").is(".type_02") ){
            $(this).find("a").wrapAll('<span></span>');
            $(this).find("img").after('<a class="drop-title">odaberi rezoluciju</a>');
        } else {
            return false;
        }
    });
    
    $("#page_03 .block dd strong").click(function(){
        $(this).closest("dd").addClass("active").siblings("dd").removeClass("active");
    });
    
    $("#page_03 .type_02 .drop-title").click(function(){
        if($(this).next("span").is(":visible")){
            $(this).next("span").slideUp();
        } else {
            $(this).next("span").slideDown();
        }
    });
});

//DOWNLOAD DROPDOWN & TAB SWITCHER
$(document).ready(function(){	
	$("#page_01 .block div").each(function(){
        if( $(this).closest("dl").is(".type_01") ){
            $(this).find("a").addClass("drop-title");
        } else if ( $(this).closest("dl").is(".type_03") ){
            $(this).find("a").wrapAll('<span></span>');
            $(this).find("img").after('<a class="drop-title">odaberi download</a>');
        } else {
            return false;
        }
    });
    
    $("#page_01 .block dd strong").click(function(){
        $(this).closest("dd").addClass("active").siblings("dd").removeClass("active");
    });
    
    $("#page_01 .type_03 .drop-title").click(function(){
        if($(this).next("span").is(":visible")){
            $(this).next("span").slideUp();
        } else {
            $(this).next("span").slideDown();
        }
    });
});

//PAGE 05 SCROLLERS
$(document).ready(function(){
    $("#page_05 .navigation_block li a").click(function(){                             
        $(this).closest("li").addClass("active").siblings("li").removeClass("active");
        $.get($(this).attr("href").replace("index.php","simple.php"), function(data){                       
             $("#page_05 .text").html(data);
             
             $(".photo_gallery a:first").click(function(){
                $(this).next("a").click();
             });
             
             $(".photo_gallery a").not(":first").lightBox();
             $("#page_05 #box_type01 .text").jScrollPane();
        },"html");
        return false;
    });
});

//PAGE 06 SCROLLERS
$(document).ready(function(){
    setupPage06();
    $("#page_02 .nav li").click(function(){                             
        $(this).addClass("active").siblings("li").removeClass("active");
        $.get($(this).attr("rel").replace("index.php","simple.php"), function(data){                       
             $("#page_02 .scroller").html(data);
             setupPage06();
        },"html");
        return false;
    });
});

function setupPage06(){
    if($("#page_02 .scroller li").length>4){
        $("#page_02 .scroller li").each(function(i){
            if(i!=0&&(i+1)%4==0){
                $(this).addClass("last");
            }
        });
        var pagesAmount = Math.ceil($("#page_02 .scroller li").length/4);
        var pageCurrent = 1;
        $("#page_02 .scroller").append('<span class="page"><b>1</b> / <b>'+pagesAmount+'</b></span><img src="images/paging-button-back.png" alt="" class="back off" /><img src="images/paging-button-next.png" alt="" class="next" />');
        $("#page_02 .scroller .back, #page_02 .scroller .next").click(function(){
            if($(this).is(".off")) { return false; }
            $("#page_02 .scroller .back, #page_02 .scroller .next").removeClass("off");
            var el = $("#page_02 .scroller ul");
            if($(this).is(".back")){
              var movement = parseFloat(el.css("margin-left"))+523;
              pageCurrent = pageCurrent-1;
            } else {
              var movement = parseFloat(el.css("margin-left"))-523;
              pageCurrent = pageCurrent+1;
            }                      
            $("#page_02 .scroller .page b:first").text(pageCurrent);
            el.animate({marginLeft:movement+"px"});
            if(pageCurrent==1){ $("#page_02 .scroller .back").addClass("off"); }
            if(pageCurrent==pagesAmount){ $("#page_02 .scroller .next").addClass("off"); }
        });
    }
}


//INSERT & SWITCH VIDEO
$(document).ready(function(){
    if($("#flash-video").html()!=null){
        setupVideo();
        $("#page_04 #video_box li a").click(function(){                             
            $(this).closest("li").addClass("active").siblings("li").removeClass("active");
            $("#myDynamicContent").remove();
            $("#video_box h3").after('<div id="flash-video" rel="'+$(this).attr("rel")+'|'+$(this).attr("href")+'"></div>');
            setupVideo();
            return false;
        });
    }
});

var playerPopupVideo;
function playerReady(obj) {
    playerPopupVideo = document.getElementById(obj['id']);
};
function setupVideo(){
    var info = $("#flash-video").attr("rel").split("|");
    var flashvars = {
      file: info[0],
      skin: "skin.swf",
      image: info[1],
      controlbar: "over"
    };
    var params = {
      wmode: "transparent",
      allowscriptaccess: "always",
      allowfullscreen: "true"
    };
    var attributes = {
      id: "myDynamicContent",
      name: "myDynamicContent"
    };
    swfobject.embedSWF("player.swf", "flash-video", "366", "300", "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

