// jQuery mod functions
jQuery.fn.center = function(){
    this.css("position", "absolute");
    //this.css("top", ($(window).height() - this.height()) / 2 - $(window).scrollTop() + "px");
    this.css("top", $(window).scrollTop() + 100 + "px");
    this.css("left", ($(window).width() - this.width()) / 2 - $(window).scrollLeft() + "px");
    return this;
}

function getImgSize(imgSrc){
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    return width;
}

//Modal Window functions
function launchWindow(id){

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    
    //Set height and width to mask to fill up the whole screen
    $('#mask').css({
        'width': maskWidth,
        'height': maskHeight
    });
    
    //transition effect
    $('#mask').fadeTo("slow", 0.6);
    $('#mask').fadeIn(500);
    
    //Get the window height and width
    //var winH = $(window).height();
    //var winW = $(window).width();
    
    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height());
    //$(id).css('left', winW / 2 - $(id).width() / 2);
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function launchPopWindow(obj){
    $('#popup_content').html('');
    var popID = $(obj).attr('rel'); //Get Popup Name
    var popURL = $(obj).attr('href'); //Get Popup href to define size
    
	//Pull Query & Variables from href URL
    var query = popURL.split('?');
    var dim = query[1].split('&');
    var popWidth = dim[0].split('=')[1];
    var popHeight = dim[1].split('=')[1];
    var popPage = dim[2].split('=')[1];
    var popElement = dim[3].split('=')[1];

    if (popElement == '') {
    	$('#popup_content').load('/' + popPage);
    }
    else {
        $('#popup_content').load('/' + popPage + ' ' + popElement);
    }
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({
        'width': Number(popWidth),
        'height': Number(popHeight)
    }).prepend('<a href="#" class="closePop"><img src="/img/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
    
    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    
    //Apply Margin to Popup
        $('#' + popID).center();
//    $('#' + popID).css({
//        'margin-top': -popMargTop,
//        'margin-left': -popMargLeft
//    });

    //Fade in Background
    //$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    //$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
    
    return false;
}

 


function launchSWFWindow(id, src){

    var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
    var majorVersion = playerVersion.major; // access the major, minor and release version numbers via their respective properties
    if (majorVersion >= 8) {
        $(id + ' #modalSWF').html('<embed src="' + src + '" width="620" height="741" allowscriptaccess="always" wmode="opaque" quality="high" bgcolor="#ffffff" name="' + id + '-swffile" id="' + id + '-swffile" style="" type="application/x-shockwave-flash">');
    }
    else {
        $(id + ' #modalSWF').html('Flash player is not installed');
    }
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    
    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width': maskWidth,
        'height': maskHeight
    });
    
    //transition effect
    $('#mask').fadeTo("slow", 0.6);
    $('#mask').fadeIn(500);
    
    //Get the window height and width
    //var winH = $(window).height();
    //var winW = $(window).width();
    
    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height());
    //$(id).css('left', winW / 2 - $(id).width() / 2);
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function launchIMGWindow(id, src, link){

    $(id + ' #modalIMG').html('<a href="' + link + '"><img class="modal-image" src="" /></a>');
    
    $(id + ' #modalIMG img.modal-image').attr('src', src).load(function(){
    
        imgWidth = getImgSize(src);
        
        $(id + ' #modalContent').css("width", imgWidth);
        $(id + ' #modalIMG').css("width", imgWidth);
        
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({
            'width': maskWidth,
            'height': maskHeight
        });
        
        //transition effect
        $('#mask').fadeTo("slow", 0.6);
        $('#mask').fadeIn(500);
        
        //Get the window height and width
        //var winH = $(window).height();
        //var winW = $(window).width();
        
        //Set the popup window to center
        //$(id).css('top', winH / 2 - $(id).height());
        //$(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).center();
        
        //transition effect
        $(id).fadeIn(2000);
        
    });
}

