$(document).ready(function(){
    if($('#intro').length > 0){
        $('#homeimages').hide();
        var intro = $('#intro');
        intro.queue(
            "introQueue",
            function( next ){
                flashembed('intro', '/assets/flash/declaration.swf');
                next();
            }
        ).delay(12000, 'introQueue');
        intro.queue(
            "introQueue",
            function( next ){
                intro.fadeOut('slow', function(){
                    $('#homeimages').fadeIn('slow');
                    homeCycle();
                    intro.remove();                
                });
                next();
            }
        );
        intro.dequeue("introQueue");
    } else {
        homeCycle();
    }
    $('#looks-landing a').hover(
        function(){
            var logo = $('<div id="lookslogo"></div>');
            $(this).append(logo);
        },
        function(){
            $('#lookslogo').remove();
        }
    );
    $('#content a[href^="http://"]').attr("target", "_blank");
});

function homeCycle(){
    if($('#homeimages').length > 0){
        var homecycle = $('#homeimages').cycle({
            speed:  'slow',
            animation: 'slow',
            easing: 'swing',
            timeout: 5000,
            pager:  '#homenav',
            before: function(){
                $('#hometitle').fadeOut();
            },
            after: function(){
                $('#hometitle').empty().append('<p>'+$(this).find('img').attr('alt')+'</p>');
                Cufon.replace('#hometitle p', { fontFamily: 'Declaration-Script', textTransform: 'none' });
                $('#hometitle').fadeIn();
            },
            pagerAnchorBuilder: function(idx, slide) { 
                return '<a href="'+$(slide).find('img').attr('src')+'" title="'+$(slide).find('img').attr('alt')+'">'+idx+'</a>'; 
            } 
        });
        $('#homeimages a').hover(
            function(){
                var logo = $('<div id="homelogo"></div>');
                $(this).append(logo);
            },
            function(){
                $('#homelogo').remove();
            }
        );
        $("#homenav a").tooltip({ 
            tip: '#hometip',
            position: 'top center',
            onBeforeShow: function(){
                var idx = this.getTrigger().index();
                var tip = $('<a href="#'+idx+'"><img src="/assets/phpThumb/phpThumb.php/146;/'+this.getTrigger().attr('href')+'" alt="'+this.getTrigger().attr('title')+'" /></a>');
                $('#hometip').empty().append(tip);
                $('#hometip a').bind('click', {idx: idx}, function(event) {
                    homecycle.cycle(idx);
                    return false;                     
                });
            }
        });
    }
}

function looksLoadDetail(picid){
    $('#looksdetail').load('/looks-detail.html', { picId : picid }, function(){
        var zoptions = {
            position : 'right',
            zoomWidth : 630,
            zoomHeight : 452,
            yOffset : 0,
            xOffset : 29,
            title :false,
            showEffect: 'fadein',
            hideEffect: 'fadeout',
            fadeinSpeed: 'slow',
            fadeoutSpeed: 'slow'
        }
//        $("#looksdetail .detail-image a").jqzoom(zoptions);
        $('#looksdetail .cloud-zoom, #looksdetail .cloud-zoom-gallery').CloudZoom();
        $('#looksdetail a.fancybox').fancybox({
            'width': 550,
            'height': 500,
            'autoDimensions': false,
            'scrolling': 'no',
            'titleShow': false,
            'onComplete': function(){
                var title = $(this.orig).attr('title');
                function setFormParams(title){
                    if($('#fancybox-frame').contents().find('#product').length > 0){
                        $('#fancybox-frame').contents().find('#product').val(title);
                    }
                    if($('#fancybox-frame').contents().find('#url').length > 0){
                        $('#fancybox-frame').contents().find('#url').val(document.location);
                    }                                
                }
                setTimeout(function(){ setFormParams(title); },1000);
            }
        });
        $('#looksdetail .back-link a').append($('#lookstitle p').text()).click(function(event){
            event.preventDefault();
            $.History.setHash('');
            $('#looksdetail').fadeOut();
        });
        Cufon.replace('#looksdetail h3, #looksdetail .detail-other p.back-link', { fontFamily: 'Declaration-Script', textTransform: 'none' });
        Cufon.now();
        if (window.addthis){
            window.addthis = null;
        }
        $.ajax({
            url: "http://s7.addthis.com/js/250/addthis_widget.js#username=edshaikh&domready=1",
            dataType: 'script',
            success: function(){
                //Global AddThis Setup
                window.addthis_config = {
                    username : 'edshaikh',
                    addthis_pub : 'edshaikh'
                }
            }
        });
    }).fadeIn();
}


function looksCarousel (itemList) {
    this.itemList = (typeof itemList == 'undefined' ? {} : itemList);

    function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt){
        // The index() method calculates the index from a
        // given index who is out of the actual item range.
        var idx = carousel.index(i, itemList.length);
        carousel.add(i, mycarousel_getItemHTML(itemList[idx - 1]));
    };
    function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt){
        carousel.remove(i);
    };
    function mycarousel_show(){
        $('#lookscarousel li:nth-child(3n)').addClass('last');
        $("#lookscarousel .detail-trigger").live('click', function(event){
            event.preventDefault();
            var picid = $(this).attr('ref');
            $.History.setHash(picid);
        });
        $("#lookscarousel").css('background','none');
        $("#lookscarousel ul").fadeIn();
        $.History.bind(function(state){
            if($.History.getHash().length > 0){
                looksLoadDetail($.History.getHash());
            } else {
                $('#looksdetail').fadeOut();
            }
        });
    };
    function mycarousel_getItemHTML(item){
        return '<a class="detail-trigger" ref="' + item.id + '" href="' + item.url + '"><img src="' + item.image + '" alt="' + item.title + '" /></a>';

    };

    $("#lookscarousel").html('<ul></ul>').find('ul').hide();
    $("#lookscarousel").jcarousel({
        scroll: 3,
        auto: 5,
        animation: 'slow',
        easing: 'swing',
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
        initCallback: mycarousel_show
    });

    return false;
};

function pressworthyCarousel() {

    function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt){
        $('#pressworthytitle p').fadeOut();
        $('.pressworthycontent').fadeOut('fast');
        $('#pressworthytitle').empty().append('<p>'+ $(item).find('img').attr('alt') +'</p>');
        Cufon.replace('#pressworthytitle p', { fontFamily: 'Declaration-Script', textTransform: 'none' });
        if(state == 'init'){
            $('.pressworthycontent').fadeIn();    
        };
    };
    function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt){
        $('#pressworthytitle p').fadeIn();
        $('.pressworthycontent').fadeIn();
    };

    $('#pressworthy li').hover(
      function () {
        $(this).find('.pressworthycontent').fadeOut();
      },
      function () {
        $(this).find('.pressworthycontent').fadeIn();
      }
    );

    $('#pressworthy').jcarousel({
        scroll: 1,
        auto: 0,
        animation: 'slow',
        easing: 'swing',
        wrap: 'both',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });

    return false;
};

function looksLanding (itemList) {
    this.itemList = (typeof itemList == 'undefined' ? {} : itemList);

    function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt){
        // The index() method calculates the index from a
        // given index who is out of the actual item range.
        var idx = carousel.index(i, itemList.length);
        carousel.add(i, mycarousel_getItemHTML(itemList[idx - 1]));
    };
    function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt){
        carousel.remove(i);
    };
    function mycarousel_show(){
        $('#lookscarousel li:nth-child(3n)').addClass('last');
        $("#lookscarousel").css('background','none');
        $("#lookscarousel ul").fadeIn();
    };
    function mycarousel_getItemHTML(item){
        return '<a href="' + item.url + '"><img src="' + item.image + '" alt="' + item.title + '" /></a>';

    };

    $("#lookscarousel").html('<ul></ul>').find('ul').hide();
    $("#lookscarousel").jcarousel({
        scroll: 3,
        auto: 5,
        animation: 'slow',
        easing: 'swing',
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
        initCallback: mycarousel_show
    });

    return false;
};

function videosCarousel() {

    function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt){
        $('#videostitle p').fadeOut();
        $('#videostitle').empty().append('<p>'+ $(item).find('h3').text() +'</p>');
        Cufon.replace('#videostitle p', { fontFamily: 'Declaration-Script', textTransform: 'none' });
    };
    function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt){
        $('#videostitle p').fadeIn();
    };

    $('#videos').jcarousel({
        scroll: 1,
        auto: 0,
        animation: 'slow',
        easing: 'swing',
        wrap: 'both',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });

    return false;
};

