var main = new function()
{
    var t = this;
    this.init = function()
    {
    	
        // load external in new page
        $("a[href^='http://']").addClass('external').attr('target', '_blank');
    	
    	
        zoomObject();
        carouselObject();
        headerCicle();
        //convertCombo();

    	
        var height = $("#container_sub").height();
        var heightMenu = $("#menu_container").height();
    	
        if (height > heightMenu)
        {
            $("#menu_container #menu").height(height-12);
            var top = height-196;
    		
            $("#menu_container #menu_footer").css({
                'position': 'absolute',
                'top': top
            });
        }

    	
    }
    
    var headerCicle = function()
    {
        
        $('.cycle').cycle({
    		fx: 'fade',
    		timeout: 6000,
    		speed: 1500,
    		random: true
    	})
    	.css('visibility', 'visible'); 
    }
    	
    var zoomObject = function()
    {
        // zoom overlay
        $(".reference_photo a").click(function(){
    		
            var title = $(this).parent('.reference_photo').siblings(".reference_left").find('h2').text();
            var images = $(this).parent('.reference_photo').siblings(".reference_images").find('ul li img');
    		
    		
            if (images.length > 0)
            {
                var index = 0;
                $("#zoom").find('.zoom_content img').attr('src', images[0].src);
	    		
                $("#zoom a.zoom_prev").click(function()
                {
                    if (index == 0) index = images.length;
                    index--;
                    $("#zoom").find('.zoom_content img').attr('src', images[index].src);
                });
                $("#zoom a.zoom_next").click(function()
                {
                    index++;
                    if (index >= images.length) index = 0;
                    $("#zoom").find('.zoom_content img').attr('src', images[index].src);
                });
            }
	    	
            $("#zoom").find('.zoom_title').html("<strong>"+title+"</strong>");
            $("#zoom").show();
            $("#zoom a.zoom_close").click(function()
            {
                $("#zoom").hide();
            });
    		
            return false;
        });
        // add key press events to zoom
        $("html").keydown(function(ev)
        {
            switch(ev.keyCode)
            {
                case 37: /*right*/
                    $("#zoom a.zoom_prev").click();
                    break;
                case 39: /*left*/
                    $("#zoom a.zoom_prev").click();
                    break;
                case 27: /*escape*/
                    $("#zoom a.zoom_close").click();
                    break;
            }
			
        });
    }
    
    var carouselObject = function()
    {
        /** Carousel **/
    	var carousel = $(".carousel .carousel_content");

        
        if (carousel.find('img').length > 3)
        {
            carousel.jCarouselLite(
            {
                btnNext: ".carousel .carousel_next",
                btnPrev: ".carousel .carousel_prev",
                visible: 3,
                speed: 300
            });

        }
        else
        {
            // disable arrows
            $(".carousel .carousel_next, .carousel .carousel_prev").click(function(){return false;});
        }

        $(".carousel .carousel_zoom ul a").click(function()
        {
            var src = $(this).find('img').attr('src');

            $(".carousel img.carousel_fullsize")
            //.attr("src", 'about:blank')
            //.css('visibility', 'hidden')
            .attr("src", src)
            .load(function()
            {
                $(this).css('visibility', 'visible');
            });

            return false;

        });
    	
        // in Internet Explorer: transparent absolute positioned anchors don't work, so add redirect to div.param children
        if ($.browser.msie)
        {
            $('div.param').each(function(index, el){

                if ($(el).find('a.item_overlay').length>0)
                {
                    var url = $(el).find('a.item_overlay').attr('href');// Get the link of the param
                    $(el).children()
                        .css('cursor', 'pointer')// Make cursor as pointer (looks like anchor that way)
                        .click(function(){
                            $(location).attr('href',url);// redirect onclick
                        });
                }
            });
        }
    }
    
    var convertCombo = function()
    {
        $('select').combobox(
        {
            comboboxContainerClass: "comboboxContainer",
            comboboxValueContainerClass: "comboboxValueContainer",
            comboboxValueContentClass: "comboboxValueContent",
            comboboxDropDownClass: "comboboxDropDownContainer",
            comboboxDropDownButtonClass: "comboboxDropDownButton",
            comboboxDropDownItemClass: "comboboxItem",
            comboboxDropDownItemHoverClass: "comboboxItemHover",
            comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",
            comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer",
            //animationType: "fade",
            width: "120px"
        });
    }
    
    
    
}

$(document).ready(function()
{
    main.init();
});		
