var active_container=null;
var sliding=false;
var hash_url="";
var current_category;

var PRODUCTS_BG="transparent url('/static/celtek_collections/images/celtek_product_main_bg.jpg') top left no-repeat";
var COLLECTIONS_BG="#2b281f none";

var detail_display_url=null;
var detail_video_links=[];

function slugify(s){
    return s.toLowerCase().replace(/[^\w0-9]+/g,'-').replace(/^-+/,'').replace(/-+$/,'');
}

function detail_page_shown(){
    $("#celtek_product_detail .display img").load(fadeInHandler);
    $("#celtek_product_detail .productvideo .videolink").click(function(){
        $("#video_container").html('<div id="display_video_embed"></div>');
        var src=$(".videosrc",this.parent).html();
        var flashvars = {videoPath:src,  accentcolor:'2890EB'};
        var params = {allowFullScreen:'true',scale: "scale",wmode: "transparent"};
        var attributes = {};
        $("#video_container").show();
        $("#celtek_product_detail .display").hide();
        swfobject.embedSWF("/static/videos/flash/Web_Cube_VideoPlayer.swf","display_video_embed","410","246","9", "/media/flash/expressInstall.swf", flashvars, params, attributes);
    });
}

function show_detail_photo(url){
    // TODO: Fix this to have smooth transition between video/photos
    $("#video_container").html("");
    
    //$(this).replaceWith('<div id="display_video_embed"></div>');
    $("#celtek_product_detail .display").fadeIn();
    //$("#celtek_product_detail .display").show();

    detail_display_url=url;
    $("#celtek_product_detail .display img").fadeOut(250,function(){
        $(this).attr("src",detail_display_url);     
    });
}

function fadeInHandler(){ $(this).fadeIn(); }

function change_hash_url(new_url){
    window.location.hash=new_url;
    hash_url=new_url;
    try{
        urchinTracker(window.location);
    }catch(err){ /*pass*/ }
}

function show_collections(){
    $(".collections").show();
    $(".category_mens").hide();
    $(".category_womens").hide();
    $("#category_listing li:visible:first").click();
    $("#collection_viewer").css("background",COLLECTIONS_BG);
    change_hash_url("/collections/");
}

function show_mens(){
    $(".collections").hide();
    $(".category_mens").show();
    $(".category_womens").hide();
    $("#category_listing li:visible:first").click();
    $("#collection_viewer").css("background",PRODUCTS_BG);
    current_category="mens";
    change_hash_url("/mens/");
}

function show_womens(){
    $(".collections").hide();
    $(".category_mens").hide();
    $(".category_womens").show();
    $("#category_listing li:visible:first").click();
    $("#collection_viewer").css("background",PRODUCTS_BG);
    current_category="womens";
    change_hash_url("/womens/");
}

function set_active_container(id){
    active_container="#"+id;
    $(active_container).css("left","0px");
    $(".collection_container").hide();
    $(active_container).show();
}

function slide_to(x){
    if(!sliding){
        sliding=true;
        $(active_container).animate({left:x+"px"},200,function(){sliding=false;});
    }
}

function slide_left(){
    var l=Number($(active_container).css("left").replace("px",""));
    var w=Number($(active_container).css("width").replace("px",""))
    if(l-840<=-w){ l=l; }else{ l-=840; }
    slide_to(l);
}

function slide_right(){
    var l=Number($(active_container).css("left").replace("px",""));
    if(l+840>0){ l=0; }else{ l+=840; }
    slide_to(l);
    
}

$(document).ready(function(){
    // resize the sliding containers 
    $('.collection_container').each(function(i){
        if($(this).hasClass("category_page")){
            $(this).css("width",(Math.ceil($('.product_thumb',this).length/2)*210)+"px");
            if($(this).css("display") == "block"){
                set_active_container(this.id);
            }
        }else{
            $(this).css("width",($('.collection_thumb',this).length*280)+"px");
            if($(this).css("display") == "block"){
                set_active_container(this.id);
            }
        }
    });

    // Rig up top tabs
    $("#toptabs .go_collection").click(show_collections);
    $("#toptabs .go_mens").click(show_mens);
    $("#toptabs .go_womens").click(show_womens);

    // left slides right.. right slides left.. so it goes
    $("#leftarrow").click(slide_right);
    $("#rightarrow").click(slide_left);

    // Rig up collections and category selectors
    $("#category_listing").hover(function(){
    });
    $("#category_listing .collections").click(function(){
        var slug=this.id.replace("collection_select_",""); 
        var name=slugify($(this).html());
        set_active_container("collection_"+slug);
        change_hash_url("/collections/"+name+"/");
        $("#category_listing li").removeClass("active");
        $(this).addClass("active");
    });
    $("#category_listing .category_nav_heading").click(function(){
        var slug=this.id.replace("category_select_",""); 
        var name=slugify($(this).html());
        $("#category_listing li").removeClass("active");
        $(this).addClass("active");
        set_active_container("category_page_"+slug);
        change_hash_url("/"+current_category+"/"+name+"/");
    });

    // setup ajax on product details
    $("a.fbajax").fancybox({
        frameWidth:916,
        frameHeight:505,
        hideOnContentClick:false,
        callbackOnShow:detail_page_shown
    });

    // preselect collections
    show_collections();
});


