﻿var MWEventGallery = null;

function MWGalleryObj(EventID, Count, Length) {
    this.gallery = EventID;
    this.count = Count;
    this.length = Length;
}

$(document).ready(function() {
	$(".modal").click(function(e) {
		e.preventDefault();
		popUp($(this));
		return false;
});

$(".modal3").click(function(e) {
    e.preventDefault();
    popUp($(this));
    return false;
});

	$("#event-list div.thumb a, #event-list h3 a").click(function(e) {
		e.preventDefault();
		$("#photo-container").empty().css({ 'height': '100px', 'width': '100px' });
		popUp($(this));
	});
	$("#photo-viewer #scroll-left a").click(function(e) {
		e.preventDefault();
		if (MWEventGallery.count > 0) {
			$("#photo-container img").fadeOut(200, function() {
				MWEventGallery.count--;
				loadPhoto(MWEventGallery.gallery, MWEventGallery.count);
			});
		}
	});
	$("#photo-viewer #scroll-right a").click(function(e) {
		e.preventDefault();
		if (MWEventGallery.count < MWEventGallery.length - 1) {
			$("#photo-container img").fadeOut(200, function() {
				MWEventGallery.count++;
				loadPhoto(MWEventGallery.gallery, MWEventGallery.count);
			});
		}
	});

	// open external links in new window
	$("a[href^=http]").each(function() {
		if (this.href.indexOf(location.hostname) == -1) {
			$(this).attr('target', '_blank');
		}
	});
});


function popUp(caller, modal) {
    var docHeight = $(document).height();
    var winHeight = $(window).height();
    var scrollHeight = $(document).scrollTop();
    var modalTop = scrollHeight;
    var callerclass = $(caller).attr("class");
    var wrapperId = "#PopUpWrapper";
    if (caller.hasClass('modal2'))
        wrapperId = "#PopUpWrapper2";
    else if (caller.hasClass('modal3'))
        wrapperId = "#PopUpWrapper3";
    else if (caller.hasClass('modal4'))
        wrapperId = "#PopUpWrapper4";

    var $wrapper = $(wrapperId); 
    // set display to block and change visibility to hidden to aquire modal height
    $wrapper.css({ 'visibility': 'hidden', 'display': 'block' });
    var modalHeight = $wrapper.height() / 2;

    if (callerclass.indexOf("array") != -1) {
        if (scrollHeight == 0) {
            if (winHeight / 2 - modalHeight <= 0) {
                modalTop = 0;
            } else {
                modalTop = winHeight / 2 - 376;
            }
        } else {
            if (scrollHeight + winHeight / 2 - modalHeight <= 0) {
                modalTop = 0;
            } else {
                modalTop = scrollHeight + winHeight / 2 - 376;
            }
        }
    } else {
        if (scrollHeight == 0) {
            if (winHeight / 2 - modalHeight <= 0) {
                modalTop = 0;
            } else {
                modalTop = winHeight / 2 - modalHeight;
            }
        } else {
            if (scrollHeight + winHeight / 2 - modalHeight <= 0) {
                modalTop = 0;
            } else {
                modalTop = scrollHeight + winHeight / 2 - modalHeight;
            }
        }
    }

    $("#ClosePop, #ClosePop2, #ClosePop3, #ClosePop4").bind("click", removePopUp);
    if (modal !== true) {
        $("#WinPopUp").css("height", docHeight).fadeTo(500, .4).click(function() {
            removePopUp();
        });
    }
    
    // set display to block and change visibility to show and display to none for fade in effect
    $wrapper.css({ 'visibility': 'visible', 'display': 'none' });

    if (modal !== true) {
        $wrapper.css("top", modalTop).fadeIn(500, function() {
            if (callerclass.indexOf("array") != -1) {
                var arrayID = callerclass.substring(callerclass.indexOf("-") + 1, callerclass.length);
                MWEventGallery = new MWGalleryObj(arrayID, 0, mwGalleryArrays[arrayID].length);
                loadPhoto(arrayID, 0);
            }
        }).click(function() {
            removePopUp();
        });
    }
    else {
        $wrapper.css("top", modalTop).fadeIn(500, function() {
            if (callerclass.indexOf("array") != -1) {
                var arrayID = callerclass.substring(callerclass.indexOf("-") + 1, callerclass.length);
                MWEventGallery = new MWGalleryObj(arrayID, 0, mwGalleryArrays[arrayID].length);
                loadPhoto(arrayID, 0);
            }
        })
    }
    
    $(".PopUpContent").click(function(e) {
        e.stopPropagation();
    });
    if (570 >= docHeight) {
        // cover the case that the popup extends below page content
        var offset = $wrapper.offset();
        var modalHeight = $wrapper.height();
        $("#WinPopUp").height(modalHeight + offset.top);
    }
};

function removePopUp() {
    $(".PopUpWrapper").fadeOut(200);
    $("#WinPopUp").fadeOut(200);
    return false;
};

function loadPhoto(id, num) {
    var curr_photo = new Image();
    var curr_title = mwGalleryArrays[id][num].Title;
    curr_photo.src = mwGalleryArrays[id][num].URL;

    if (MWEventGallery.count == 0) {
        $("#photo-viewer #scroll-left").hide();
    } else {
        $("#photo-viewer #scroll-left").show();
    }
    if (MWEventGallery.count == MWEventGallery.length - 1) {
        $("#photo-viewer #scroll-right").hide();
    } else {
        $("#photo-viewer #scroll-right").show();
    }
    
    $("#photo-footer h3").text(curr_title);
    $("#photo-curr").text(MWEventGallery.count+1);
    $("#photo-total").text(MWEventGallery.length);

    /*var loader = setTimeout(function() { animateImage(curr_photo, curr_title) }, 1000);*/
    $("#photo-container").html(curr_photo);
    $("#photo-container img").load(function() {
        $(this).hide();
        $("#photo-container").animate({
            width: curr_photo.width,
            height: curr_photo.height
        }, 500, function() {
            $("#photo-container img").fadeIn(200);
        });
    }).error(function() {
        $("#photo-container").html('<p>There was an error trying to load the photo.</p>');
        $("#photo-container").append(curr_photo);
    });
}

// tracking outbound links
function recordOutboundLink(link, category, action) {
    try {
        var myTracker = _gat._getTrackerByName();
        _gaq.push(['myTracker._trackEvent', ' + category + ', ' + action + ']);
        setTimeout('window.open("' + link.href + '");', 100)
    } catch (err) { }
}
