/**
 * @author shikeb
 */
var interval; // will serve as a common handle for switching the show on/off.
var period = 5000; //in milliseconds
var numPics; //number of images in the slideshow

var curIdx = 0; // currently displayed image index
var playing = false;
var textDisplay = false;

var slidesLoaded = false;
var slidesCheckInterval; // handle for the interval at which to check whether images have been loaded for the slideshow
var carCheckInterval;

$('document').ready(function(){

    $('#play').click(function(){
        if(slidesLoaded){
            repeat();
        }
    });
    $('#pause').click(function(){
        if(slidesLoaded){
            stop();
        }
    });
    $('#next').click(function(){
        if(slidesLoaded){
            stop();
            setView((numPics>curIdx+1)?++curIdx:(curIdx = 0));
        }
    });
    $('#previous').click(function(){
        if(slidesLoaded){
            stop();
            setView((curIdx==0)?(curIdx = numPics-1):--curIdx);
        }
    });
    $('.jMyCarousel ul a').click(function(e){
	    return false;
	});
    /*    $('.jMyCarousel ul a').each(function(i){
	    $(this).attr('href','#');
        $(this).click(function(e){
            e.preventDefault();
            if(slidesLoaded){
                stop();
                $('#view .storyDescription').remove();
                setView(i % numPics);
                curIdx = i % numPics;
            }
            return false;
        });
    });
    */
    //LOADING POPUP
    //Click the button event!
    $("div.hidDiv").hide();
    $("#description").click(function(){
        //centering with css
        id = 2;
        centerPopup(id);
        //load popup
        loadPopup(id);
        if(playing){
            stop();
        }
        textDisplay = true;
    });

    //CLOSING POPUP
    //Click the x event!
    $(".popupContactClose").click(function(){
        disablePopup(id);
    });
    //Click out event!
    $(".backgroundPopup").click(function(){
        disablePopup(id);
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup(id);
        }
    });
    if($('#view').length){
        init();
    }
});

function init(){
    // integrated with jMyCarousel output
    numPics = $('#slideShowContent img').length;
    slidesCheckInterval = setInterval(checkSlidesLoaded,period / 2);
    carCheckInterval = setInterval(vCenterCarouselImages, 1000);
    var initVisible = $('#view .selected').attr('id').substr(5);

    curIdx = initVisible * 1;
    setView(curIdx);
    $('#view li').hover(function(){
        $(this).children('.imgDesc').animate({
                'height' : '50px'
            }, 500);
        },function(){
            $(this).children('.imgDesc').animate({
                'height' : '0px'
            }, 500);
    });

    $(".jMyCarousel").jMyCarousel({
        'visible':'83%',
        'evtStart':'mousedown',
        'evtStop':'mouseup',
        'btnPrev':$('#jmcPrev'),
        'btnNext':$('#jmcNext'),
        'start': curIdx * 150,
        'eltByElt' : true
    });


}

function repeat(){
    if(!playing){
        play(); // start playback to avoid the  period ms delay
        interval = setInterval(play,period);
    }
}

function play() {
    if(slidesLoaded){
        if(textDisplay){
            $('#view .storyDescription').remove();
            $('li#slide'+curIdx).show();
            textDisplay = false;
        }

        playing = true;

        if(numPics > (curIdx + 1) ){
            curIdx++;
        } else {
            curIdx = 0;
        }

        // fadeOut the current image in the view and
        // fadeIn the next one when the fadeOut finishes.
        $('#viewInner').fadeOut(period/4,function(){
            setView(curIdx);
            $('#viewInner').fadeIn(period/4);
        });
    }
}

function stop(){
    clearInterval(interval);
    playing = false;
}

function setView(idx){

    $('#view li').hide();
    $('li#slide' + idx).show();
    var id = $('li#slide' + idx).attr('rel');
    
    if(typeof(http_path) != 'undefined'){
        $('#bigBuy a').attr('href', http_path +"/carts/index/"+id);
    }
    if(textDisplay){
        textDisplay = false;
    }
}

function checkSlidesLoaded(){
    var loadedAll = true;
    $('#slideShowContent img').each(function(i){
        if(!this.complete){
            loadedAll = false;
        }
    });

    if(loadedAll){
        slidesLoaded = true;
        $('#view').removeClass('waiting');

        // Centering the slideshow images vertically
        var parent = $('#view li');
        var selected = $('#view .selected').attr('id').substr(5);
        $('#view li img').each(function(i){
          if(i != (selected * 1)){
            $(this).parent('li').show();
          }
          vCenterImage(parent, this);
          if(i != (selected *1)){
            $(this).parent('li').hide();
          }
        });

        clearInterval(slidesCheckInterval);
    }

}


/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(id){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup"+id).css({
            "opacity": "0.7"
        });
        $("#backgroundPopup"+id).fadeIn("slow");
        $("#popupContact"+id).fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(id){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup"+id).fadeOut("slow");
        $("#popupContact"+id).fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(id){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact"+id).height();
    var popupWidth = $("#popupContact"+id).width();
    //centering
    $("#popupContact"+id).css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6

    $("#backgroundPopup"+id).css({
        "height": windowHeight
    });
}

function vCenterCarouselImages(){
    if(imagesLoaded){
        clearInterval(carCheckInterval);
        var parent = $('.jMyCarousel a');
        $('.jMyCarousel img').each(function(i){
           vCenterImage(parent, this);
        });
	
	$('.jMyCarousel ul a').each(function(i){
	    $(this).attr('href','#');
	    $(this).click(function(e){
		    e.preventDefault();
		    if(slidesLoaded){
			stop();
			$('#view .storyDescription').remove();
			setView(i % numPics);
			curIdx = i % numPics;
		    }
		    return false;
		});
	    });
    }
}