(function($){
    var openedRef=null;
    var options = {beforeClose:function(){},
            beforeOpen:function(){},
            afterOpen:function(){}};
    $.fn.slideButton = function(obj){
        obj = $.extend({
            beforeClose:function(){},
            beforeOpen:function(){},
            afterOpen:function(){}
        }, obj || {});
       //$(this).children(".buttons_content ul").hide();
       //var parent = $(this).parent();
       var self = $(this);
      // $(self).css({"overflow":"hidden"});
      // $(self).find("a:first").css({"position":"absolute","z-index":"5"});
       var content = $(this).children(".buttons_content");
       //$(content).css({"position":"absolute","z-index":"4", "width":"988px","left":"-989px"});
        if($.browser.msie && $.browser.version==7)  $(content).find(".carousel").hide(); //for ie7
        $(self).click(function(){
            if(openedRef!=content)
                close(openedRef);
        });
        $(this).children(".openslide").click(function(){
            if(openedRef != content )
            {

                close(openedRef);
                openedRef = content;
                options = $.extend(obj);
               
                $(".buttons_closure").unbind("click");
                $(".buttons_closure").click(function(){close(content)});

                obj.beforeOpen();
                //$(".buttons_closure").show().click();
                $(self).css({"width": "1024"});//for ie
                //$(content).show(10, function(){
                    $(content).animate({"left":"35px"}, 1500, function(){obj.afterOpen();}); //, function(){ $(this).children(".buttons_content").children().show(); }
               // });
                
                $(".buttons_closure").show();
            }
            else{
                close(content);
            }
        }/*
    function(){

    }*/);

       function close(element){
                 //alert("!");
                 openedRef = null;
                 options.beforeClose();
                

                 $(".buttons_closure").hide();
                 $(element).animate({"left":"-989px"}, 1500,
                             function(){
                                 //$(element).parent().css("width","30px");
                                 // $(element).hide();
                             });                  
       }
    }
})(jQuery);

(function($){
    
    
    $.fn.Carousel = function(obj){
        var current = 0;
        var ulSize = $(this).find(".carousel_content ul li").size();
        var liWidth = $(this).find(".carousel_content li").width() + 20; //with offsets
        var ul =  $(this).find(".carousel_content ul");
        var right = $(this).children(".toright");
        var left = $(this).children(".toleft");
          obj = $.extend({
            visible : 3,
            step : 1,
            ulSize:ulSize,
            liWidth:liWidth
        }, obj || {});
        return this.each(function(){
        /*declaration section*/
          
            //var container =  $(parent + ".carousel_content");           
            /*$.fn.Init = function (){Init(this, obj)};*/
            $.fn.Open = function (){Open(this)};
            $.fn.Close = function (){Close(this)};
             /*init section*/
             Close(this);
             Init(this, obj);
             
            /*action section*/
            $(right).click(function(){
                if(ulSize >= obj.visible+obj.step+current)
                {
                    $(ul).animate({"left":-((obj.step+current)*liWidth)+"px"}, 300);
                    current +=obj.step;
                }
                else if(ulSize > current+obj.visible)
                {
                    step = ulSize - current-obj.visible;

                    $(ul).animate({"left":-((step+current)*liWidth)+"px"}, 300);
                    current += step;
                }
                if(current>0)
                    $(left).css({"background-image":"url('/imgs/prettyPhoto/previous.png')"});

            });
            $(left).click(function(){
                if(current-obj.step >= 0)
                {
                    $(ul).animate({"left":-((current-obj.step)*liWidth)+"px"}, 300);
                    current -=obj.step;
                }
                else if (current > 0){

                    $(ul).animate({"left":"0px"}, 300);
                    current = 0;
                }
                if(current >= 0)
                    $(left).css({"background-image":"none"});
            });
        })
    }
  
   /*$.fn.CarouselEvents = function(event){
       switch(event)
       {
           case "Init" : Init(this); break;
           case "Open" : Open(this); break;
           case "Close" : Close(this); break;
           default:break;
       }
   }*/
   function detectSize(object){
       return {
           liWidth: $(object).find(".carousel_content li").width() + 20,
           ulSize: $(object).find(".carousel_content ul li").size()
       };
   }
   function Init(object,prop){
                opt = detectSize(object);
                
                $(object).find(".carousel_content").css({"width" : (prop.visible*opt.liWidth) + "px"});
                $(object).find(".carousel_content ul").css({"width" : (opt.ulSize*opt.liWidth) + "px"});
               
            }
   function Open(object){
                opt = detectSize(object);
                if(8 < opt.ulSize)
                {                   
                    $(object).find(".toleft").show();
                    $(object).find(".toright").show();
                }
                $(object).fadeIn(300);
            }
   function Close(object){
                 $(object).fadeOut(300, function(){
                     $(object).find(".toleft").css({"background-image":"none"});
                     $(object).find(".toleft").hide();
                     $(object).find(".toright").hide();
                     $(object).css({"left":"0px"});
                 });
   }


 
})(jQuery);


$(document).ready(function(){
  
  
   //alert($(window).height()+" " +$(".page").height());
   $(".commercial .carousel").Carousel({
            visible : 8,
            step : 2
    });

   $(".projects .carousel").Carousel({
            visible : 8,
            step : 2
    });

    $(".main_buttons.projects").slideButton({
        
        beforeClose:function(){
            $(".projects .carousel").Close();
            if($.browser.msie && $.browser.version==7)
             $(".projects .carousel").hide();
        },
        afterOpen:function(){
            $(".projects .carousel").Open();
            if($.browser.msie && $.browser.version==7)
            $(".projects .carousel").show();
        }
    });
    $(".main_buttons.commercial").slideButton({
        
        beforeClose:function(){
            $(".commercial .carousel").Close();
            if($.browser.msie && $.browser.version==7)
             $(".commercial .carousel").hide();
        },
        afterOpen:function(){
            $(".commercial .carousel").Open();
            if($.browser.msie && $.browser.version==7)
            $(".commercial .carousel").show();
        }
    });
    $(".main_buttons.contact").slideButton({
        beforeClose:function(){
            $(".contact .buttons_content div").fadeOut();
            
        },
        afterOpen:function(){
            $(".contact .buttons_content div").fadeIn();
        }
    });
    /*$(".carousel_content").jCarouselLite({
        btnNext: ".toleft",
        btnPrev: ".toright",
        visible: 8,
        circular:false
        });*/
    $(".content .main_content").height($(window).height()*0.8 < 600? 600 : $(window).height()*0.8);//70 percent height
    $(".content .main_content").jScrollPane({
        wheelSpeed:40
    });

 if( $(window).height()-25 > $(".description").offset().top && $(window).height() < $(".page").height())
   {      
       $(".page").css("height",($(".page").height()+($(window).height()-$(".description").offset().top-25))+"px");
   }
   else if($(window).height() > $(".page").height())
       $(".page").height($(window).height());


})


