﻿var slide_moving = false;

jQuery(document).ready(function(){
    //loop through items and add special stuff
    initiate_slideshow();
    jQuery("#hotscot_slideshow").before('');
});

function nextwebsite(){
    var lastitem = jQuery("#hotscot_slideshow a").size()-1;
    moveslideitem(lastitem,jQuery("#hotscot_slideshow a:eq(" + lastitem+ ")").attr("href"));
}

function initiate_slideshow(){
    var mainheight = 276;
    jQuery("#hotscot_slideshow a").each(function(itm){
        jQuery(this).unbind('hover').bind('hover', function(e) {
            e.preventDefault();
        });
        jQuery(this).unbind('click').bind('click', function(e) {
            e.preventDefault();
        });
        jQuery(this).unbind('click.fb').bind('click.fb', function(e) {
            e.preventDefault();
        });
            
        jQuery(this).children("div.hidden_slide_bg").remove();
        if(itm!=0){
            jQuery(this).addClass("hidden_slide");
            //calculate top
            var top = 276 + (64*(itm-1));
            mainheight = mainheight + 64;
            jQuery(this).css("top",top + "px");
            jQuery(this).append('<div class="hidden_slide_bg"><!-- EMPTY --></div>');
            jQuery(this).hover(function(){
                    jQuery(this).children("div.hidden_slide_bg").remove();
                },function(){
                    if(jQuery(this).children("div.hidden_slide_bg").size()==0){
                    jQuery(this).append('<div class="hidden_slide_bg"><!-- EMPTY --></div>');
                    }
                });
            jQuery(this).click(function(){
                moveslideitem(itm,jQuery(this).attr("href"));
            });
        }else{
            jQuery(this).fancybox();
        }
    });
    
    jQuery("#hotscot_slideshow").css("height",mainheight + "px");
}

function moveslideitem(itm,val){
    if(!slide_moving){
        slide_moving = true;
        //fold main slide
        jQuery("#hotscot_slideshow a:eq(0) img").animate({margin:"-212px 0px 0px 0px"},200,function(){
            jQuery("#hotscot_slideshow a:eq(0)").append('<div class="hidden_slide_bg"><!-- EMPTY --></div>');
            jQuery("#hotscot_slideshow a:eq(0)").animate({top:"212px"},500,function(){
                //move selected slide up
                jQuery("#hotscot_slideshow a:eq(" + itm + ") .hidden_slide_bg").remove();
                jQuery("#hotscot_slideshow a:eq(" + itm + ")").animate({top:"0px"},500,function(){
                    jQuery("#hotscot_slideshow a:eq(" + itm + ") img").css("margin","0px 0px 0px 0px");
                        jQuery("#hotscot_slideshow a:eq(" + itm + ")").animate({height:"276px"},500,function(){
                            //redo this item for topitem
                            var aelem = '<a href="' + val + '" target="_blank">' + jQuery("#hotscot_slideshow a:eq(" + itm + ")").html() + '</a>';
                            jQuery("#hotscot_slideshow a:eq(" + itm + ")").remove();
                            jQuery("#hotscot_slideshow").prepend(aelem);
                            
                            initiate_slideshow();
                            slide_moving = false;
                    });
                });
                
                //push everything else down
                for(itms=0;itms<itm;itms++){
                    var itmstop = jQuery("#hotscot_slideshow a:eq(" + itms + ")").css("top").replace("px","");
                    itmstop = (itmstop-0) + 64;
                    jQuery("#hotscot_slideshow a:eq(" + itms + ")").animate({top:itmstop + "px"},500);
                }                     
            });
        });
    }
}
