$(document).ready(function(){
    $("body").addClass("js");
});

//HEADER GALLERY
$(document).ready(function(){
   var headerGalleryLength = $("#header img").length;
   var currImg=0;
   if(headerGalleryLength!=1){
      $("#header img").hide();
      $("#header img").each(function(i){
          $(this).css("z-index",headerGalleryLength-i);
      });
      $("#header img").show();
      setInterval(rotateGallery,7000);
   }
   
   function rotateGallery(){
      if(currImg==(headerGalleryLength-1)){
          $("#header img:first").fadeIn("slow",function(){
              $("#header img").show();
              currImg=0;
          });
      } else {
          $("#header img:eq("+currImg+")").fadeOut("slow");
          currImg=currImg+1;
      }
   }
});

//TEXT GALLERY
$(document).ready(function(){
    $(".text .gallery li a").click(function(){
        $(this).parents("li:first").siblings("li").find("img").css("opacity","1");
        $(this).find("img").css("opacity","0.65");
        $(this).parents(".text:first").find(".img-right").attr("src",$(this).attr("href"));
        return false;
    });
});

//DROPDOWN
$(document).ready(function(){
    $(".lang a").click(function(){
        if($(".dropdown_box_02").html()!=null){ closeSelectDrop02(); }
        if($(".dropdown_box").html()!=null){ closeSelectDrop(); return false; }
        $("body").append('<div class="dropdown_box"></div>');
        var el = $(this);
        var offset = el.offset();
        var offsetTop = offset.top+el.height();
        var offsetLeft = offset.left;
        var contentEl = $(this).parents("dl").clone();
        var content="";
        contentEl.find("dd").each(function(){
            content = content+"<li>"+$(this).html()+"</li>";
        });
        $(document).click(function(e){ if(!$(e.target).is(".dropdown_box")&&$(e.target).parents(".dropdown_box").attr("class")!="dropdown_box"){ closeSelectDrop(); } });
        $(".dropdown_box").html('<ul>'+content+'</ul>').css({top:offsetTop,left:offsetLeft,width:el.parents("dd").width()}).slideDown();
        return false;
    });

    $("#sidebar ol a").click(function(){
        if($(".dropdown_box").html()!=null){ closeSelectDrop(); }
        if($(".dropdown_box_02").html()!=null){ closeSelectDrop02(); return false; }
        $("body").append('<div class="dropdown_box_02"></div>');
        var el = $(this);
        var offset = el.offset();
        var offsetTop = offset.top+el.height()+3;
        var offsetLeft = offset.left+2;
        var content = $(this).parents("ol").clone();
        $(document).click(function(e){ if(!$(e.target).is(".dropdown_box_02")&&$(e.target).parents(".dropdown_box_02").attr("class")!="dropdown_box_02"){ closeSelectDrop02(); } });
        $(".dropdown_box_02").html('<ul>'+content.html()+'</ul>').css({top:offsetTop,left:offsetLeft}).slideDown();
        return false;
    });
    
    function closeSelectDrop(){
        $(document).unbind("keypress");
        $(".dropdown_box").slideUp(function(){
            $(this).remove();
        });
    }
    
    function closeSelectDrop02(){
        //$(document).unbind("keypress");
        $(".dropdown_box_02").slideUp(function(){
            $(".dropdown_box_02").remove();
        });
    }   
});


//HISTORY
$(document).ready(function(){
    if($(".about-us").html()!=null){
        $('#happenings').wrap('<div id="happenings-wrapper"></div>');
    		var element1 = $("#happenings li");
    		var timeElement1 = $("#years li");
    		var sliderWidth1 = element1.length*(parseFloat(element1.width()) + parseFloat(element1.css("margin-right")) + parseFloat(element1.css("border-right-width")) + parseFloat(element1.css("border-left-width")));
    		var timelineWidth1 = timeElement1.length*(parseFloat(timeElement1.width()) + parseFloat(timeElement1.css("margin-left")));
    		
    		$('#happenings').before('<div id="slider-1"><div class="ui-slider-handle"></div></div>')
    		$("#happenings").width(sliderWidth1+"px");
    		sliderWidth1 = sliderWidth1-$("#happenings-wrapper").width()-parseFloat(element1.css("margin-right"));
    		
    		$("#years").width(timelineWidth1+"px");
    		timelineWidth1 = timelineWidth1 - $('#years').parent().width();
    
    		$("#slider-1").slider({ 
    			slide: function(e,ui) {			
    				$("#happenings").css("margin-left",-(sliderWidth1*(ui.value/100)));
    				if (timeElement1.length > 12) {
    					$("#years").css("margin-left",-(timelineWidth1*(ui.value/100)));
    				}
    			}
    		});
    		
    		$("#maraska-through-history #happenings .scroll").jScrollPane();
        
                
    		$('#products-through-history #products').wrap('<div id="products-wrapper"></div>');
    		var element2 = $("#products-through-history #products li");
    		var timeElement2 = $("#centuries li");
    		var sliderWidth2 = element2.length*(parseFloat(element2.width()) + parseFloat(element2.css("padding-right")));
    		var timelineWidth2 = timeElement2.length*(parseFloat(timeElement2.width()));
    		
    		$('#products-through-history #products').before('<div id="slider-2"><div class="ui-slider-handle"></div></div>')
    		$("#products-through-history #products").width(sliderWidth2+"px");
    		sliderWidth2 = sliderWidth2-$("#products-wrapper").width()-parseFloat(element2.css("margin-right"));
    		
    		var overamount = $("#products-through-history #products li").length;
    		
    		$("#centuries").width(timelineWidth2+"px");
    		timelineWidth2 = timelineWidth2 - $('#centuries').parent().width();

    		$("#slider-2").slider({ 
    			slide: function(e,ui) {
    				$("#products-through-history #products").css("margin-left",-(sliderWidth2*(ui.value/100)));
    				if (timeElement2.length > 12) {
    					$("#centuries").css("margin-left",-(timelineWidth2*(ui.value/100)));
    				}
    			}
    		});
    }		
});

