var slide_count_home = 5;
var slide_count_work = 9;

var slide_width_home = 640;
var slide_width_work = 886;

var slide_speed = 375;

var slide_count;
var slide_width;

var cur_slide = '1';
var prefix = '';
var lock_click = 0;

var popupStatus = 0;

var popupReady = 0;

function changeSelection(elm){
    $('#pop_pagenumbers a').removeClass('selected');
    elm.addClass('selected');
}

function slide(direction) {
    
    
    if(lock_click == 0){

        lock_click = 1;
        
        var jump = 0;

        if(direction == 'next'){
            direction = -1;
        } else {
            direction = 1;
        }

        var cur_pos = $("#pop_slide #" + prefix).css('left').replace('px', '');

        var new_pos = parseInt(cur_pos) + (direction * slide_width);

        var new_left = slide_count * slide_width * -1;

        //going prev
        if(direction == 1){

            //at the first, need to go to last

            if(new_pos == 0){

                cur_slide = slide_count;
                jump = 1;

            } 
        //going next
        } else {

            //at the last, need to go to the first
            
            if(new_pos == (new_left - slide_width)){
  
                new_left = -1 * slide_width;
                cur_slide = 1;

                jump = 1;

            }
        }

        if(jump){

            $("#pop_slide #" + prefix).animate(
            {
                left: new_pos + 'px'
            },
            slide_speed
            ).animate(
            {
                left: new_left + 'px'
            },
            0,
            function(){
                lock_click = 0;
            }
            );

        } else {


            $("#pop_slide #" + prefix).animate(
            {
                left: new_pos + 'px'
            }, 
            {
                duration: slide_speed, 
                easing: "easeInOutSine", 
                complete: function(){
                    lock_click = 0;

                }
            }
            );

            cur_slide = parseInt(cur_slide) - (direction * 1);

        }

        
        changeSelection($('#slide_link_' + cur_slide));


    }

}

function set_track(slide){

    if(!slide){
        slide = 1
    }

    changeSelection($('#slide_link_' + slide));

    var track_width = (slide_count + 3) * slide_width;
    $("#pop_slide #" + prefix).css('width', track_width + 'px');
    
    var track_pos = slide_width * slide * -1;
    $("#pop_slide #" + prefix).css('left', track_pos + 'px');

    cur_slide = slide;

}

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){

        $("#ajax_loader").css('display', 'block');
        $("#ajax_loader").css('position', 'fixed');
        $("#ajax_loader").centerInClient({ forceAbsolute: true });

        $("#pop_overlay").css({
            "opacity": "0.7"
        });
        $("#pop_overlay").fadeIn("fast");
        $("#ajax_loader").show();

        if(popupReady == 1){
            $("#pop_container").centerInClient({ forceAbsolute: true });
            $("#pop_container").fadeIn("fast");
        }
        
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    $("#ajax_loader").hide();
    
    if(popupStatus==1){

        $("#pop_overlay").fadeOut("fast");
        $("#pop_container").fadeOut("fast");
        popupStatus = 0;
    }
}

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow
    ///  and attached to the body element to ensure proper absolute positioning.
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);

    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

function showPopUp() {

    popupReady = 1;

    $("#popup").show();
  
    $("#ajax_loader").show();

    var width = $("#popup").css('width');
    var height = $("#popup").css('height');

    $("#pop_container").width(width);
    $("#pop_container").height(height);


    $("#pop_container").centerInClient({ forceAbsolute: true });

    if(popupStatus == 1){
        $("#pop_container").fadeIn("slow");
    }
}


$(document).ready(function() {

    var include = '';
    var loc = location.href;

    if(loc.indexOf('prior') > -1){
        include = 'work';
    } else {
        include = 'home';
    }

    $("#pop_container").load('includes/pop-' + include + '-ajax.php', function() {

        $("#popup").find('img').batchImageLoad({
                loadingCompleteCallback: showPopUp
        });

    });

    $("#pop_slide").css('overflow', 'hidden');

    $(".popup_link a").live("click", function() {

        var id = $(this).parent().get(0).id;
        var last = id.lastIndexOf('_');
        prefix = id.substring(last + 1, id.length );

        if (prefix == 'pw'){

            slide_count = slide_count_work;
            slide_width = slide_width_work;

        } else {
            slide_count = slide_count_home;
            slide_width = slide_width_home;
        }

        set_track($(this).attr('id'));

        $("#pop_slide ul").hide();
        $("#pop_slide #" + prefix).show();

        $("#pop_footer").removeClass('tt');
        $("#pop_footer").removeClass('pla');
        $("#pop_footer").removeClass('ee');

        $("#pop_footer").addClass(prefix);

        loadPopup();

        return false;

    });

    $('#pop_pagenumbers a').live('click', function() {

        id = $(this).attr('id');
        var last = id.lastIndexOf('_');
        
        cur_slide = id.substring(last + 1, id.length );

        var new_left = cur_slide * slide_width * -1;

        $("#pop_slide #" + prefix).animate(
        {
            left: new_left + 'px'
        },
        0
        );
        changeSelection($(this));
        
    });

    $("#popup #next a").live('click', function(){

        slide('next');
        return false;
    });

    $("#popup #prev a").live('click', function() {
            slide('prev');
            return false;
        }
        );

    $("#popup #close a").live('click', function() {

        disablePopup();

        return false;

    });

    $("#pop_overlay").live('click', function() {

        disablePopup();

    });

});
