$(document).ready(function () {
    var explainerBoilerplate = "Explore more of the WestMUN site and of other related sites by clicking on the buttons below.";

$("img.rollover").hover(
    function (){
        this.src = this.src.replace("_off", "_on");        
    }, function() {
        this.src = this.src.replace("_on", "_off");
    });
//

//
    function storifyIn() {
        $("#explainer").text("Read stories about the UN from citizen journalists.");
    }

    function otherIn() {
        $("#explainer").text("Click here to see a nifty dark background with a modal overlay.");
    }

    function splashButtonOut() {
        $("#explainer").text(explainerBoilerplate);
    }
    $("#moreSplashStuff").addClass('pointerClass').hover(otherIn, splashButtonOut);
    $("#button1").addClass('pointerClass').hover(otherIn, splashButtonOut);
    $('#button4').addClass('pointerClass').hover(storifyIn, splashButtonOut).click(function () {
        window.open("http://storify.com/un", '_blank');
    });
    $('.nonInnerBaseBar').addClass('pointerClass').click(function () {
        window.open("http://www.facebook.com/groups/14120572158/", '_blank');
    });
    var randyNum = Math.floor(Math.random() * 1001);
    $('.signerUpper').addClass('pointerClass').click(function () {
        window.open("signerUp/signerUp.html?rn=" + randyNum, '_self');
    });

    function loadButtons() {
        $('.downerLoader0').addClass('pointerClass').click(function () {
            window.open("downloads/1stCommittee.pdf?rn=" + randyNum, '_blank');
        });
        $('.downerLoader1').addClass('pointerClass').click(function () {
            window.open("downloads/securityCouncil.pdf?rn=" + randyNum, '_blank');
        });
        $('.downerLoader2').addClass('pointerClass').click(function () {
            window.open("downloads/whoReport.pdf?rn=" + randyNum, '_blank');
        });
        $('.downerLoader3').addClass('pointerClass').click(function () {
            window.open("downloads/human.pdf?rn=" + randyNum, '_blank');
        });
        $('.downerLoader4').addClass('pointerClass').click(function () {
            window.open("downloads/criminal.pdf?rn=" + randyNum, '_blank');
        });
        $('.downerLoader5').addClass('pointerClass').click(function () {
			
            window.open("downloads/historic.pdf?rn=" + randyNum, '_blank');
        });
		 $('.downerLoader7').addClass('pointerClass').click(function () {
			
            window.open("downloads/elements.pdf?rn=" + randyNum, '_blank');
        });
		 $('.downerLoader8').addClass('pointerClass').click(function () {
			
            window.open("downloads/rulesOf.pdf?rn=" + randyNum, '_blank');
        });
		 $('.downerLoader9').addClass('pointerClass').click(function () {
			
            window.open("downloads/rome.pdf?rn=" + randyNum, '_blank');
        });
    }
    loadButtons();
	function loadTheWorkingDox () {
		$("img.rollover").hover(
    function (){
        this.src = this.src.replace("_off", "_on");        
    }, function() {
        this.src = this.src.replace("_on", "_off");
    });
	//rejigger
	$('.downerLoader6').addClass('pointerClass').click(function () {
        var divHt = $("#committees").height();
        var divWd = $(".container").width();        
        popper0.css({
            "margin-top": "0",
            "padding": "24px",
            "display": "inherit"
        });
        committees.fadeOut('slow', function () {
            committees.replaceWith(popper0).show();
           
        });
		});
	//
	}
var popper0 = $("#popper0");
var committees = $("#committees");
var committeeHolder = $("#committeeHolder");
//moved xout
 $(".workingDox").click(function () {
	// alert("1");
               /* popper0.replaceWith(committees);
                committees.show();
                */
				popper0.hide();
				//alert("2");
				committeeHolder.append(committees);
				//alert("3");
				committees.show();
				//alert("4");
				loadButtons();
				//scab in the re-rollover
				$('img').each(function() {
this.src = this.src.replace("_on", "_off");
});
				//end scab in the re-rollover
				
				$("img.rollover").hover(
    function (){
        this.src = this.src.replace("_off", "_on");        
    }, function() {
        this.src = this.src.replace("_on", "_off");
    });
	loadTheWorkingDox ();
            });
			//end moved xout
    $('.downerLoader6').addClass('pointerClass').click(function () {
        var divHt = $("#committees").height();
        var divWd = $(".container").width();        
        popper0.css({
            "margin-top": "0",
            "padding": "24px",
            "display": "inherit"
        });
        committees.fadeOut('slow', function () {
            committees.replaceWith(popper0).show();
           /* $(".workingDox").click(function () {
                popper0.replaceWith(committees);
                committees.show();
                loadButtons();
            });*/
        });
    });
    $("#button1").click(function (e) {
        $('.window').show();
        e.preventDefault();
        var id = $("#dialog");
        //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').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('left', winW / 2 - $(id).width() / 2);
        //transition effect
        $(id).fadeIn(2000);
        $("input:text:visible:first").focus();
    });
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
    });
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });
    $("#captcha").focus(function () {
        $("#captcha").text("");
    });

    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }
    var entryCaptcha = "";
    $("#sendGloria").click(function () {
        var entryName = $("input#entryName").val();
        var entryEmail = $("textarea#entryEmail").val();
        var entryMessage = $("textarea#entryMessage").val();
        if ($("input#entryName").val() == "") {
            $("input#entryName").val("Please enter your name.");
            $("input#entryName").select();
            return false;
        } else {
            entryName = $("input#entryName").val();
        }
        if (isValidEmailAddress($("input#entryEmail").val())) {
            entryEmail = $("input#entryEmail").val();
        } else {
            $("input#entryEmail").val("Please enter a valid email address.");
            $("input#entryEmail").select();
            return false;
        }
        var thisC = $("#captcha").val();
        var aString = $("input#entryName").val();
        var thatC = aString.slice(0, 1);
        if (thisC != thatC) {
            $("#captcha").val("");
            alert("Please enter the first letter of the name that you entered above.");
            $("#captcha").val("Please enter the first letter of the name that you entered above.");
            $("#captcha").select();
            return false;
        } else {
            entryCaptcha = thisC;
        }
        var dataString = 'entryName=' + entryName + '&entryEmail=' + entryEmail + '&entryMessage=' + entryMessage + '&entryCaptcha=' + entryCaptcha;
        //
        $.ajax({
            type: "POST",
            url: "scripts/contactGloriaA.php",
            data: dataString,
            success: function (msg) {
                //return code here. Code commented out until the godaddy scripting issues are resolved.
                if (msg == "1") {
                    $("#stylized").html("<div id='message'></div>");
                    $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>An response email has been sent to your address to confirm this submission. Please do not reply to the email. If you do not receive the response email, please contact <em>webmaster@westmun.org</em>.</p><p>Click anywhere withing the gray area to dismiss this message");
                } else {
                    $("#stylized").html("<div id='message'></div>");
                    $('#message').html("<h2>An Error Occurred</h2>").append("<p>" + msg + "</p><p>Please notify the webmaster at webmaster@westmun.org.</p><p>Click anywhere within the gray area to dismiss this message.");
                }
            }
        });
        return false;
    });
    $("#moreSplashStuff").click(function () {
        $(".later").show();
        $(this).hide();
    });
    //signup stuff here
    //end signup stuff
    //get the docs stuff here
    /*$("#generalDoc").on("click", function (){
		alert("YO");
	//window.open("downloads/GeneralAssembly1stCommitteeDirectorsReport.pdf", '_blank');
	});*/
    /*$("#generalDoc").click(function() {
		window.open("downloads/1stCommittee.pdf", '_blank');
	});*/
    //end get the docs stuff
});
