// JavaScript Document

var products = ['a', 'b', 'c', 'd', 'e'];
var curt = 'down';
var moving = false;

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
function arrinc2(arr,obj) {
    for(var i=0; i<arr.length; i++) {
        if (arr[i] == obj) return i;
    }
 }

function pload(mparam){                
      var cont = $("#cont");
      var event;
        
      var id = "i"+mparam;
      var contId = mparam;
      
      var abc = $("#s"+mparam);      
      abc.addClass("hil");
      
      //////////////
      
      if(arrinc2(products, contId) == 1){
          return false;
      } 
        
      var cDiv = $("#"+contId);
      
      var pos = products[1];
      var pos = $("#"+pos).position();
        
      //
      cDiv.fadeOut(500, function(){
          if(contId=="a" || contId =="b"){
             cDiv.addClass("classUp");
             cDiv.find("img").css("height","309px");
          }
      
          cDiv.css('position', 'absolute');
          cDiv.css('left', pos.left);
          cDiv.find('img').prop('src', root+'img/'+contId+'Big.png');
          
          var origId = products[1];
          var orig = $("#"+origId);
          orig.find('img').prop('src', root+'img/'+origId+'Small.png');
          if(orig.hasClass("classUp")){ 
            orig.removeClass("classUp");
            orig.find("img").css("height","266px"); 
          }
          
          $("img").ifixpng(root+"pixel.gif");
          
          // vymenit pozice
          cDiv.addClass("big");
          orig.removeClass("big");
          cDiv.animate({ "left": ''+pos.left+'px' }, 10, function(){
          cDiv.fadeIn(900, function(){

              

              var claim = $("#claim");                
              
              if (isIE && isWin && !isOpera){
                 claim.css("top", "35px");                
                 claim.css("display", "block");
              }else{
                claim.fadeIn("slow");
              }

              
              var cont = $("#cont");
              /*$.get(root+'templates/data.php?pid='+contId, function(data){
                cont.html(data);
                cont.slideDown();
              });*/
              cont.load(root+'templates/data.php', {pid: contId}, function(data){
                $(this).slideDown();
                var spoustec = $("#s"+mparam);
                spoustec.addClass('hil');
                //spoustec.css("border", "1px solid black");
              });
              
              /*
              .slideDown( function(){
                var spoustec = $("#s"+mparam);
                spoustec.addClass('active');
                spoustec.css("border", "1px solid black");

          } )*/  
          });
            
            ///////// update products order
            switch (contId) {
                case 'a':
                    products = ['e','a','b','c','d'];
                    break;
                case 'b':
                    products = ['a','b','c','d','e'];
                    break;
                case 'c':
                    products = ['b','c','d','e','a'];
                    break;
                case 'd':
                    products = ['c','d','e','a','b'];
                    break;
                case 'e':
                    products = ['d','e','a','b','c'];
                    break;
            }
            for(var i=0; i<products.length; i++) {
                if(i!=1){
                    var tmp = products[i];
                    var tmp = $("#"+tmp);
                    var left = i*200;
                    
                    tmp.css('position', 'absolute');
                    tmp.css('left', left);
                }
            }
        });
        });
        
        return false;
   }
   
$(document).ready(function(){
    var claim = $("#claim");
    var sfl = $("#splashflash");
    sfl.css("display", "block");
    
                           
    function curtain(reset){

    var body = $("body");
    var products = $("#wrap");
    var cont = $("#cont");

    bgpos = '-200px';
    prodpos = '-150px';

    if(reset == true){
          
            
        bgpos = '0px';
        prodpos = '100px';
    }


    if(curt == 'down' || reset == true){
        
      if (navigator.appName == 'Microsoft Internet Explorer'){ 
           body.css('backgroundPositionY', bgpos);
       }else{
            body.animate({
                backgroundPosition: "(50% "+bgpos+")"
            }, {duration: 400}, function(){

            });
       }

       products.animate({
            'top': prodpos
        }, 400);
        
        var tmp = products.position();
        //alert(tmp.top);
        
        //cont.css("borderTop", "1px solid black");
        
            
        if(reset != true){
            curt = 'up';
        }else{
            
            
            orderProd();
  
        }
            
    }
    if(reset == true){
        curt = 'down';
            
    }
        
    return false; 
}
    function arrinc(arr,obj) {
        for(var i=0; i<arr.length; i++) {
            if (arr[i] == obj) return i;
        }

    }
    
    function productsMove(event, a, b){
        var gl = $("#glowSmall");
        gl.css("display", "none");
        var glB = $("#glowBig");
        glB.css("display","none"); 
           
        var id = event.target.id;
        if(a != null){ id = a; }
        var contId = $(this).prop('id');
        if(b != null){ contId = b; }
        
        
        var flash = $("#splashflash");
        flash.css("display", "none");
        
        /// pokud se klikne na produkt, ktery je na druhe pozici (cili se poradi nemeni)
        if(arrinc(products, contId) == 1){
          curtain();
          
          $("#"+contId).addClass("big");
        
          var claim = $("#claim");       
          if (isIE && isWin && !isOpera){
            //claim.css("top","0px");
            //claim.css("left","0px");
            
            claim.css("display", "block");
          }else{
            claim.fadeIn("slow");
            
            var smfl = $("#smallflash");
            smfl.css("display", "block");
          }        
          var cont = $("#cont");
          cont.load(root+'templates/data.php', {pid: contId}, function(){
            $(this).slideDown();
            var spoustec = $("#s"+contId);
            spoustec.addClass('hil');
          } ); /* .slideDown( function(){
            
          });*/
          return false;
        }   //////////////////////////////////////////////////////////////////
        
        var cDiv = $("#"+contId);
        
        var pos = products[1];
        var pos = $("#"+pos).position();
                  
        //  vymenit produkty
        cDiv.fadeOut(500, function(){
            if(contId=="a" || contId =="b"){
               cDiv.addClass("classUp");
               cDiv.find("img").css("height","309px");
            }
            
            cDiv.css('position', 'absolute');
            cDiv.css('left', pos.left);
            cDiv.find('img').prop('src', root+'img/'+contId+'Big.png');
            
            var origId = products[1];
            var orig = $("#"+origId);
            orig.find('img').prop('src', root+'img/'+origId+'Small.png');
            if(orig.hasClass("classUp")){ 
              orig.removeClass("classUp");
              orig.find("img").css("height","266px"); 
            }
            
            $("img").ifixpng(root+"pixel.gif");
            
            // vymenit pozice
            cDiv.addClass("big");
            orig.removeClass("big");
            cDiv.animate({ "left": ''+pos.left+'px' }, 10, function(){
            cDiv.fadeIn(900, function(){
          
                curtain();
                
                
                
                
                var claim = $("#claim");
                if (isIE && isWin && !isOpera){                                
                   claim.css("display", "block");                
                }else{
                  claim.fadeIn("slow");
                   var smfl = $("#smallflash");
                smfl.css("display", "block");
                }  
                
                var cont = $("#cont");
                
                cont.load(root+'templates/data.php', {pid: contId}, function(){
                  $(this).slideDown();
                  var spoustec = $("#s"+contId);
                  spoustec.addClass('hil');
                  var sfl = $("#splashflash");
                  sfl.hide();
                } ); /*.slideDown( function(){
                  
                  flash.css("display", "none");
                  //var spoustec = $("#s"+id);
                  //spoustec.css("border", "1px solid black");
                  
               } );    */
            });
            
            ///////// update products order
            switch (contId) {
                case 'a':
                    products = ['e','a','b','c','d'];
                    break;
                case 'b':
                    products = ['a','b','c','d','e'];
                    break;
                case 'c':
                    products = ['b','c','d','e','a'];
                    break;
                case 'd':
                    products = ['c','d','e','a','b'];
                    break;
                case 'e':
                    products = ['d','e','a','b','c'];
                    break;
            }
            for(var i=0; i<products.length; i++) {
                if(i!=1){
                    var tmp = products[i];
                    var tmp = $("#"+tmp);
                    var left = i*200;
                    
                    tmp.css('position', 'absolute');
                    tmp.css('left', left);
                }
            }
            
            
        });
        });      
        
        return false;
    }
    
    function rstCurt(){
      curt = "down";
      var body = $("body");
      var products = $("#wrap");
      
      var tmptmp = $(".big");
      tmptmp.removeClass("big");
      
      bgpos =  "0px";
      prodpos = "47px";

      var claim = $("#claim");
      claim.css("display","none"); 
      
      var smfl = $("#smallflash");
          smfl.css("display", "none");
        
        if (navigator.appName == "Microsoft Internet Explorer"){ 
           body.css("backgroundPositionY", bgpos);
       }else{
            body.animate({
                backgroundPosition: "(50% "+bgpos+")"
            }, {duration: 400}, function(){
                
            });
            
       }
       products.animate({
            "top": prodpos
        }, 400, function(){
          
        
          var fl = $("#splashflash");
            fl.css("display", "block");
        });
        
      
      $("#cont").fadeOut();
    }
    
    /////////
    function hideMore(event){
      var image = $("#more");
      image.css("display", "none");
    }
    
    var fl = $("#splashflash");
    var pos = $("#header").position();
    fl.css("left", pos.left+325+"px");
    
    $(window).resize(function(){
      var fl = $("#splashflash");
      var pos = $("#header").position();
      var claim = $("#claim");
      
      var smfl = $("#smallflash");
      
      fl.css("left", pos.left+325+"px");
      claim.css("left", pos.left+472+"px");
      smfl.css("left", pos.left+332+"px");
    });
    
    function glowOn(event){
      var obj = $(this);
      var pos = obj.position();
      var obj2 = $("#wrap");
      var pos2 = obj2.position();
     
      if(obj.attr("id") == "a" || obj.attr("id") == "b"){
        var glow = $("#glowBig");
        
        glow.css("top", pos2.top + (pos.top + 50) + "px");
        glow.css("left", pos2.left + (pos.left - 10) + "px");
      }else{
        var glow = $("#glowSmall");
        
        glow.css("top", pos2.top + (pos.top - 60) + "px");
        glow.css("left", pos2.left + (pos.left + 5) + "px");
      }
      
      glow.css("display", "block");
    }
    
    function glowOff(event){
      var obj = $(this);
      if(obj.attr("id") == "a" || obj.attr("id") == "b"){
        var glow = $("#glowBig");
      }else{
        var glow = $("#glowSmall");
      }
   
      glow.css("display", "none");
    }
    
    $(".prod").bind('click', productsMove);
    $(".prod").bind('mouseover', glowOn);
    $(".prod").bind('mouseout', glowOff);
    
    $("#logobt").bind("click", rstCurt);
    
   
    var obj2 = $("#wrap");
    var pos2 = obj2.position();
    var posh = $("#header").position();
    
    if (isIE && isWin && !isOpera){
      claim.css("top", "34px"); 
    }else{
      claim.css("top", posh.top + 26 +"px");
    } 
    
    claim.css("left", posh.left+472+"px");
  
    $("#cont").fadeOut(); 
    
    var smfl = $("#smallflash");
    smfl.css("left", posh.left+332+"px");
    smfl.css("top", "-26px");  
              
});
