$(document).ready(function () {
					
    // navigation menu
					
    function megaHoverOver(){
        $(this).find(".sub").stop().fadeTo('fast', 1).show();
            //Calculate width of all ul's
            (function($) {
                jQuery.fn.calcSubWidth = function() {
                    rowWidth = 0;
                    //Calculate row
                    $(this).find("ul").each(function() {
                        rowWidth += $(this).width();
                    });
                };
            })(jQuery);
						
	if ( $(this).find(".row").length > 0 ) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function() {
                $(this).calcSubWidth();
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({'width' :biggestRow});
            $(this).find(".row:last").css({'margin':'0'});
							
        } else { //If row does not exist...
            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({'width' : rowWidth});
        }
    }
					
    function megaHoverOut(){
        $(this).find(".sub").stop().fadeTo('fast', 0, function() {
            $(this).hide();
	});
    }
				
				
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	timeout: 500, // number = milliseconds delay before onMouseOut    
	out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };
				
    $("ul#mega_nav li .sub").css({'opacity':'0'});
    $("ul#mega_nav li").hoverIntent(config);
														
    // homepage slideshow
					
    $("#slideshow").easyAccordion({
        autoStart: true,
	slideInterval: 5000
    });
					
    // product detail information tabs
					
    $( "#tabs" ).tabs();
				
    // product detail slideshow and fancy box
					
    var a = function(self){
        self.anchor.fancybox();
    };

    var imgArr = [];
    function setGallery() {
        $("#pikame li").each(function(i) {
            var img = $(this).find('a').attr('href');
            var title = $(this).find('img').attr('alt');
            imgArr[i] = {'href': img, 'title': title};
//            $(this).find('a').attr("href", "javascript:;");
            $(this).find('img').attr("title", i);
        });
    }
    setGallery();
    
    function showGallery(num) {
        $.fancybox(imgArr, {'index': num, 'titlePosition': 'inside', 'type': 'image'});
    }
    function updateImage(self) {
        var num = self.active.attr("title");
        //console.log(num, imgArray);
        //self.caption.find("a").click(function(){showGallery(num)}); // Uncomment to activate popup on any links in caption
        self.anchor.click(function(){showGallery(num)});
    }


    $("#pikame").PikaChoose({autoPlay: false, showCaption: false, transition:[0], buildFinished:a, animationFinished:updateImage});
    if ($("#pikame").children().length < 4) {
        $('.pika-textnav').hide();
    }

//    if ($("#pikame").children().length < 1) {
//        this.hide();
//    }

    $("#pikame").jcarousel({
        scroll:4,
        initCallback: function(carousel) {
            $(carousel.list).find('img').click(function() {
                //console.log($(this).parents('.jcarousel-item').attr('jcarouselindex'));
                carousel.scroll(parseInt($(this).parents('.jcarousel-item').attr('jcarouselindex')));
            });
	}
    });

    $("#prod-videos").jcarousel({
        buttonNextHTML: '<div>Next</div>',
        buttonPrevHTML: '<div>Previous</div>'
    });

    $('a[rel="prod-videos"]').click(function () {
        $.fancybox({
            'padding'		: 0,
            'autoScale'		: false,
            'transitionIn'	: 'none',
            'transitionOut'	: 'none',
            'title'			: this.title,
            'width'		: 680,
            'height'		: 495,
            'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'			: 'swf',
            'swf'			: {
                'wmode'		: 'transparent',
                'allowfullscreen'	: 'true'
            }
        });

        return false;
    });

});

