

$(document).ready(function () {


    //Get Mouse Position
    var MouseX = 0;
    var MouseY = 0;
    $(document).mousemove(function (e) {
        MouseX = e.pageX;
        MouseY = e.pageY;
    });



    //Initialize Overlay
    $("#overlayC").css("position", 'absolute');
    $("#overlayC").css("top", '0px');
    $("#overlayC").css("left", '0px');
    $("#overlayC").css("z-index", '1001');
    $("#overlayC").css("width", '100%');
    $("#overlayC").css("height", '1px');
    $("#overlayC").css("display", 'none');
    $("#overlayC").css("background", '#333'); //adjust color as needed


    //initialize lightbox
    $("#lightBox").css("position", 'absolute');
    $("#lightBox").css("z-index", '1002');


    //Initialize Close Button
    $("#closeBtn").css("position", 'absolute');
    $("#closeBtn").css("z-index", '1003');
    $("#closeBtn").css("width", '69px'); //adjust width as needed
    $("#closeBtn").css("height", '35px'); //adjust height as needed


    //Mouse Click for LightBox Item
    $('.lightBoxItem').click(function (e) {

        //prevent Click link action
        e.preventDefault();
		
		//turn on overlayC
		$("#overlayC").css("display", 'block');
		
		//refrence the object being clicked on
        var getObj = $(this);

        //Find the total Window Height
        var windowHeight = $(document).height();

        //re-set overlay to the full height of window
        $("#overlayC").css("height", windowHeight + "px");

        //re-set lightbox settings
        $("#lightBox").css("width", '10px');
        $("#lightBox").css("height", '10px');
        $("#lightBox").css("margin-left", '0px');
        $("#lightBox").css("top", (MouseY - 5) + 'px');
        $("#lightBox").css("left", (MouseX - 5) + 'px');
        $("#lightBox").css("padding-top", '10px');


        //find type of content and properties from title tag located in anchor tag
        var contentWidth = getVariables(getObj.attr('alt'), "w")
        var contentHeight = getVariables(getObj.attr('alt'), "h")
		
		//Load Content
		videoRef = "#" + getObj.attr('href');
        videoCode = $(videoRef).html();
        videoCode = videoCode.replace('<!--', "");
        videoCode = videoCode.replace('-->', "");
        $("#container").html(videoCode);
		
		
		//function that handles parsing out variables stored in title tag
        function getVariables(path, property) {
            var fullurl = path
            var param = (fullurl.slice(0))

            var pairs = param.split("&")
            var targetvar = property

            for (var i = 0; i < pairs.length; i++) {
                var pos = pairs[i].indexOf('=')
                if (pos == -1) continue

                var argname = pairs[i].substring(0, pos)
                var value = pairs[i].substring(pos + 1)
                if (argname == targetvar) {
                    var searchstring = value
                    { break }
                }
            }

            return searchstring
        }        
   

        //fadeIn page overlay BG
        $("#overlayC").css("opacity", "0.0");
        $("#overlayC").animate({ "opacity": "0.85" }, 500, function () {
		    //animate lightbox
			animateBox();
        });


        //set the top of the box postion
        scrollTop = $(window).scrollTop(); //Get scroll postion
        lightBoxMaxTopPos = 50 + scrollTop;


		

		//animate box
        function animateBox() {
			$("#lightBox").css("display", "block");
			
			$("#lightBox").animate({
                "width": Number(contentWidth) + 20 + "px", //width of lightbox
                "height": Number(contentHeight) + 10 + "px", //height of lightbox
                "top": lightBoxMaxTopPos + "px", //adjust top position of light box
                "left": "50%", //center lightbox on page
                "margin-left": "-" + (Number(contentWidth) / 2) + "px" // adjust center position
            }, 500, function () {
                
				//First anmiation completed load other animations
                
				$('#container').slideDown('slow')
				$("#container").css("display", 'block');

				//add close button
                turnOnCloseBtn();
				
            });
			
        }


        //set Close Button
        function turnOnCloseBtn() {
            $("#closeBtn").css("display", 'block');
            var lightBoxPos = $("#lightBox").offset();
			$("#closeBtn").css("left", '50%');
			$("#closeBtn").css("margin-left", ((Number(contentWidth) / 2) - 70) + 'px');
			//$("#closeBtn").css("right", (lightBoxPos.left - 25) + 'px');
            $("#closeBtn").css("top", (lightBoxPos.top - 25) + 'px');
        }


    });//close button tag


    //close button for lightbox
    $(document).keyup(function (e) {
        if (e.keyCode == 27) {
            closeLightBox();
        }
    });



});


//close button functionality for lightbox
function closeLightBox() {
        $("#container").html("");
	    $("#container").css("display", 'none');
        $("#closeBtn").css("display", 'none');
		$("#lightBox").slideUp("Fast");
		$("#overlayC").animate({"opacity": "0.0"}, 1500, function(){
				$("#overlayC").css("height", '1px');				
		});
	}


	
	
	
