$(document).ready(function(){
    $(".wrapper ul li:last").css('margin-right', '0px');
    preloader();
    var totalHeight = $(window).height() - ($("#footer_container").outerHeight())
    $("#content_container").height(totalHeight + 102);
    $(".content,.next_wrapper").height(totalHeight + 102);
    $('.instructionsButton').live('mouseenter', function(){
        $(this).css('color', 'white')
    });
    $('.instructionsButton').live('mouseleave', function(){
        $(this).css('color', '#CCC')
    });
    
    $(".instructionsButton").live('click', function(){
        $(".appDetails").fadeIn(250);
    });
    $(".closeWindow").live('click', function(){
        $(".appDetails").fadeOut(250);
    });
    
	$(".closeWindow").live('mouseenter',function() {
		$(this).css('background-position','0px -20px');
	});
	
	$(".closeWindow").live('mouseleave',function() {
        $(this).css('background-position','0px 0px');
    });
	
	
    $(window).resize(function(){
        var totalHeight = $(window).height() - ($("#header_container").outerHeight() + $("#footer_container").outerHeight())
        $("#content_container, .content, .next_wrapper,.news").height(totalHeight + 102);
        if ($(window).height() < 800) {
            $(".home").css('background-position', '50% 38%');
            $(".home").css('background-image', 'url(/images/trees2.png)');
            $("#logo").width(255).height(220);
            $("#mailbox").width(188).height(318);
            $(".textWrapper").css({
                'margin-right': '254px',
                'top': '74px'
            });
            $(".locationBlurb").css({
                'top': '-255px',
                'left': '366px'
            });
        }
        else {
            $(".home").css('background-position', '50% 60%');
            $(".home").css('background-image', 'url(/images/trees.png)');
            $("#logo").width(307).height(249);
            $("#mailbox").width(380).height(624);
            $(".textWrapper").css({
                'margin-right': '71px',
                'top': '371px'
            });
            $(".locationBlurb").css({
                'top': '0px',
                'left': '550px'
            });
        }
        
    });
    
    var animating = false;
    
    $("#nav_bottom .homeLink").click(function(){
        if ($(this).hasClass('current') || animating) {
            return false;
        }
        animating = true;
        
        $(".current").removeClass("current");
        $(this).addClass("current");
        
        homeSetup();
    });
    
    $("#nav_bottom a").not(":last").click(function(){
        loadPages($(this).attr('name'));
    });
    
    $("#header_container ul li").mouseenter(function(){
        if (animating) {
            return false;
        }
        
        $(".artistBlurbWrapper").css('left', '');
        $(".artistBlurbWrapper").css('right', '');
        var $$ = $(this);
        
        var posLeft = $$.position().left;
        $(".artistBlurbWrapper").hide();
        
        $(".artistBlurbWrapper").load("/artists/" + $$.index() + "blurb.html", function(){
            $(".artistBlurbWrapper dl:odd").css('background-color', '#FFF').css('opacity', .5);
            
            if (posLeft > 570) {
                $(".artistBlurbWrapper").css('left', (posLeft + 60) - $(".artistBlurbWrapper").width()).show();
            }
            else {
                $(".artistBlurbWrapper").css('left', posLeft).show();
            }
        });
        
    });
    
    $("#header_container ul li").mouseleave(function(){
        $(".artistBlurbWrapper").hide();
    });
    
    $("#header_container ul li").click(function(){
        if ($(this).hasClass('current') || animating) {
            return false;
        }
		$("#slides").remove();
        animating = true;
        $(".artistBlurbWrapper").hide();
        $(".current").removeClass("current");
        $(this).addClass("current");
        $(".activeLink").fadeOut(function(){
            $(this).remove();
        });
        $("#nav_bottom li a").css('color', '#000');
        
        var artist = $(this).find('a').attr('name');
        
        $("<div class='next_wrapper'></div>").appendTo("#content_container").load("/artists/" + $(this).index() + ".html", function(){
            $('.fancybox').fancybox();
            $(".artistsWrapper").height(totalHeight);
            $('.artistsWrapper').jScrollPane();
            $("<div class='enlargeInfo'></div").insertAfter($(".artistImage"));
            
            $(".enlargeInfo").text('Click to enlarge').css("opacity", .5);
            $(this).height(totalHeight + 102);
            $(this).css('top', totalHeight + 102);
            $('.next_wrapper .zig').hide().css('top', '-10px').show();
            
            
            if ($(this).index() == 1) {
                $("#slides").slides({
                    play: 5000,
                    pagination: false,
                    effect: 'fade'
                });
            }
            
            
            
            
            $("#content_container > div").animate({
                top: '+=' + (totalHeight + 112) * -1
            }, 1000, 'easeInQuad', function(){
                animating = false;
                if ($("#content_container").children().length > 1) {
                    $("#content_container > div:first-child").remove();
                }
                $(".content").remove();
                
                $("dl:odd").css('background-color', '#fd7d00').css('opacity', .8);
                
                
                $(".artistImage").attr('src', '/images/' + artist + '.jpg');
                $(".fancybox").not("#slides .fancybox").attr('href', '/images/' + artist + '-large.jpg');
                
                initialize(artist);
                $(".jspTrack").fadeIn(1000);
                
            });
            
            
        });
    });
    $(window).load(function(){
        homeSetup();
        $(".tempContainer").remove();
    });
    
    $("#contactForm .submitButton").live('click', function(){
        validateForm();
    });
    
    $(".applicationSubmit").live('click', function(){
        validateApplicationForm();
    });
    
    
    $("#content_container").mousemove(function(){
        $(".artistBlurbWrapper").hide();
    });
    
    function loadPages(el){
        if ($("." + el + "Link").hasClass('current') || animating) {
            return false;
        }
        animating = true;
		
        $(".current").removeClass("current");
        $("." + el + "Link").addClass("current");
        if ($.browser.msie) {
            $(".activeLink").remove();
        }
        else {
            $(".activeLink").fadeOut(function(){
                $(this).remove();
            });
        }
        $("#nav_bottom li a").css('color', '#000');
        $("<div class='next_wrapper'></div>").appendTo("#content_container").load("/html/" + el + ".html", function(){
        
        
            if (el == "news") {
                $(".yossmancredits").css('opacity',.1);
				$(".news").height(totalHeight);
                var i = 1;
                $(".news div").each(function(){
                    if ($(this).index() / i == Math.round($(this).index() / i)) {
                        $(this).css('clear', 'left').addClass('divime');
                        i = i + 3;
                    }
                });
                $(".divime").not(":first").each(function(){
                    $("<div class='divider'></div>").insertBefore($(this));
                });
                
                $('.news').jScrollPane();
            }
            
            
            
            
            if (el == "supporters") {
            
                var url = "";
                
                $('ul.supportersList').shuffle().show();
                $('ul.supportersList a').fancybox({
                    onComplete: function(){
                        if (url == "") {
                            return false;
                        }
                        else {
                            $('#fancybox-img').wrap($("<a href=" + url + " target='_blank'>"));
                        }
                    }
                });
                $(".supportersList").children().each(function(){
                    $(this).find('a').attr('href', $(this).find('img').attr('src'));
                });
                
                $('ul.supportersList a').click(function(){
                    url = $(this).attr('alt');
                });
				
				$(".supporters").height(totalHeight);
                $('.supporters').jScrollPane();
                
            }
            
            if (el == "contact") {
                $(".contact").height(totalHeight);
                $('.contact').jScrollPane();
            }
            
            if (el == "application") {
                $(".addressWrapper dl:odd").css('background-color', '#FFF').css('opacity', .3);
                $(".application").height(totalHeight);
                
                $('.application').jScrollPane();
                imageUploader();
            }
            
            
            
            if (el == "ack") {
                $(".ack li:odd").css('background-color', '#FFF').css('opacity', .5);
                $(".ack").height(totalHeight);
                $('.ack').jScrollPane();
                
                $(".committee .list:first").children("li").each(function(){
                    var storeIndex = $(this).index();
                    if ($(this).height() > $(".committee .list:last li:eq(" + storeIndex + ")").height()) {
                        $(".committee .list:last li:eq(" + storeIndex + ")").height($(this).height())
                    }
                    else {
                        $(this).height($(".committee .list:last li:eq(" + storeIndex + ")").height())
                    }
                    
                });
            }
            
            if (el == "archives") {
                $(".archives  li:odd").css('background-color', '#FFF').css('opacity', .5);
            }
            
            
            if (el == "map") {
                $(".ack").height(totalHeight);
                $('.ack').jScrollPane();
				initializeAllMap();
            }
            
            
            
            $(this).height(totalHeight + 102);
            $(this).css('top', totalHeight + 102);
            $('.next_wrapper .zig').hide().css('top', '-10px').show();
            $("<div class='activeLink'></div>").appendTo($("." + el + "Link").parent());
            if ($.browser.msie) {
                $(".activeLink").show();
            }
            else {
                $(".activeLink").fadeIn();
            }
            $("." + el + "Link").css('color', '#555');
            $("#content_container > div").animate({
                top: '+=' + (totalHeight + 112) * -1
            }, 1000, 'easeInQuad', function(){
                $(".artistBlurbWrapper").hide();
                animating = false;
                if (el == "contact" || el == "application") {
                    showRecaptcha();
                }
                $(".jspTrack").fadeIn(1000, function(){
                
                });
                if ($("#content_container").children().length > 1) {
                    $("#content_container > div:first-child").remove();
                }
                $(".content").remove();
            });
            
            
        });
    }
    
    
    
    
    
    
    function homeSetup(){
        if ($(".content").length == 0) {
            $("<div class='content'></div>").prependTo($("#content_container"));
        }
        animating = true;
        $(".next_wrapper").fadeOut(function(){
            $(this).remove();
        });
        $(".content").load('home.html', function(){
            if ($(window).height() < 800) {
                $(".home").css('background-position', '50% 38%');
                $(".home").css('background-image', 'url(/images/trees2.png)');
                $("#logo").width(255).height(220);
                $("#mailbox").width(188).height(318);
                $(".textWrapper").css({
                    'margin-right': '254px',
                    'top': '74px'
                });
                
                $(".locationBlurb").css({
                    'top': '-255px',
                    'left': '366px'
                });
            }
            else {
                $(".home").css('background-position', '50% 60%');
                $(".home").css('background-image', 'url(/images/trees.png)');
                $("#logo").width(307).height(249);
                $("#mailbox").width(380).height(624);
                $(".textWrapper").css({
                    'margin-right': '71px',
                    'top': '371px'
                });
                $(".locationBlurb").css({
                    'top': '0px',
                    'left': '550px'
                });
            }
            $(".contentWrapper").height(totalHeight);
            $("#mailbox").css('top', totalHeight).show();
            $("#logo").show();
            $(".zig").css('top', '4px').show();
            if ($.browser.msie) {
                $(".activeLink").remove();
            }
            else {
                $(".activeLink").fadeOut(function(){
                    $(this).remove();
                });
            }
            $("<div class='activeLink'></div>").appendTo($(".homeLink"));
            if ($.browser.msie) {
                $(".activeLink").show();
            }
            else {
                $(".activeLink").fadeIn();
            }
            
            
            $(".homeLink").css('color', '#555');
            if ($(window).height() < 625) {
                $(".home").css('background-position', '50% 18%');
            }
            if ($.browser.msie) {
                $(".home").show();
                
                $("#logo").animate({
                    top: '7px'
                }, 1050, 'easeOutBounce', function(){
                    $(".wrapper ul li").show();
                    
                });
                
                $("#mailbox").animate({
                    top: '146px'
                }, 550, 'easeOutQuad', function(){
                
                });
                $(".textWrapper").fadeIn(1000, function(){
                    animating = false;
                });
                $(".locationBlurb").fadeIn();
            }
            else {
                $(".home").fadeIn(1000, function(){
                    $("#logo").animate({
                        top: '7px'
                    }, 1050, 'easeOutBounce', function(){
                        $(".wrapper ul li").show();
                        
                    });
                    
                    $("#mailbox").animate({
                        top: '166px'
                    }, 550, 'easeOutQuad', function(){
                    
                    });
                    $(".textWrapper").fadeIn(1000, function(){
                        animating = false;
                    });
                    $(".locationBlurb").fadeIn();
                });
            }
            
            
            
            
        });
    }
    
    
    function validateForm(){
        var emailCheck = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;
        $(".submit").attr('disabled', false);
        $(".errMsg").remove();
        $(".inputText,textarea").css({
            'background-color': '#FFF'
        });
        
        var username = $("input#name").val();
        
        var valid = true;
        
        if ($("#contactForm input#name").val() == '') {
            $("<p style='color:red' class='errMsg'>You must enter something here.</p>").insertBefore($("#contactForm input#name"));
            valid = false;
        }
        
        if ($("#contactForm textarea#message").val() == '') {
            $("<p style='color:red' class='errMsg'>You must enter something here.</p>").insertBefore($("#contactForm textarea#message"));
            valid = false;
        }
        
        if ($("input#email").val().match(emailCheck)) {
            valid = true;
        }
        else {
            valid = false;
            $("<p style='color:red' class='errMsg'>This is a not a valid email address.</p>").insertBefore($("#contactForm input#email"));
            return false;
        }
        
        if (valid) {
            $(".submit").attr('disabled', true);
            $.post("/verify.php", $("#contactForm").serialize(), function(data){
                if (data.success) {
                    $(".formContact").children().fadeOut(1000, function(){
                        $(".formContact").empty();
                        $('<h2 class="ty">Thank you ' + username + '</h2><p class="ty">We will get back to you as soon as possible!</p>').appendTo(".formContact").fadeIn();
                        $(".submit").attr('disabled', false);
                    });
                }
                else {
                    $(".whoops").remove();
                    $('<div class="whoops"><h1>Whoops!</h1><p>Something went wrong, did you pick the right two words?</p></div>').insertAfter(".inputTextArea");
                    valid = false;
                    $(".submit").attr('disabled', false);
                }
            }, "json");
        }
        else {
            return false;
        }
    }
    
    
    function validateApplicationForm(){
        var emailCheck = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;
        $(".submit").attr('disabled', false);
        $(".errMsg").remove();
        $(".inputText,textarea").css({
            'background-color': '#FFF'
        });
        
        var username = $("input#name").val();
        
        var valid = true;
        
        
        
        
        $("#applicationForm input").not("#email").not("#recaptcha_response_field").not(".notRequired").each(function(){
            if ($(this).val() == '') {
                valid = false;
                $("<p style='color:red' class='errMsg'>This is a required field.</p>").insertBefore($(this));
            }
        });
        
        if ($("#applicationForm textarea#description").val() == '') {
            $("<p style='color:red' class='errMsg'>You must enter something here.</p>").insertBefore($("#applicationForm textarea#description"));
            valid = false;
        }
        
        if ($("input#email").val().match(emailCheck)) {
            valid = true;
        }
        else {
            valid = false;
            $("<p style='color:red' class='errMsg'>This is a not a valid email address.</p>").insertBefore($("#applicationForm input#email"));
            return false;
        }
        
        if (valid) {
        
            if (!imagesUploaded) {
                alert('You must upload at least 1 image for review.')
                valid = false;
                return false;
            }
            
            $(".submit").attr('disabled', true);
            $.post("/applicationVerify.php", $("#applicationForm").serialize() + "&filenames=" + filenames.join(","), function(data){
                if (data.success) {
                    $(".formContact").children().fadeOut(1000, function(){
                        $(".formContact").empty();
                        $('<h2 class="ty">Thank you ' + username + '</h2><p class="ty">We will process this application and get back to you as soon as possible!</p>').appendTo(".formContact").fadeIn();
                        $(".submit").attr('disabled', false);
                    });
                }
                else {
                    $(".whoops").remove();
                    $('<div class="whoops"><h1>Whoops!</h1><p>Something went wrong, did you pick the right two words?</p></div>').insertAfter(".inputTextArea");
                    valid = false;
                    $(".submit").attr('disabled', false);
                }
            }, "json");
        }
        else {
            return false;
        }
    }
    
    
    
    
    
    
    
    $(".errMsg, input, textarea").live('click', function(){
        $(".errMsg").remove();
    });
    
    function showRecaptcha(){
        Recaptcha.create("6LeoDbwSAAAAAKIzRbTF1ETWsEjrP9KU1p2Ey6eM", 'contactFormAJAX', {
            theme: "white",
            callback: Recaptcha.focus_response_field
        });
    }
    
    var imagesUploaded = false;
    var filenames = [];
    
    function imageUploader(){
        $('#file_upload').uploadify({
            'uploader': '/uploadify/uploadify.swf',
            'script': '/uploadify/uploadify.php',
            'cancelImg': '/uploadify/cancel.png',
            'folder': '/uploads',
            'auto': true,
            'fileExt': '*.jpg;*.gif;*.png',
            'fileDesc': 'Image Files',
            'multi': true,
            'buttonImg': '/images/browse.png',
            'width': 117,
            'height': 40,
            'wmode': 'transparent',
            'onComplete': function(event, ID, fileObj, response, data){
                filenames.push(fileObj.name);
            },
            'onAllComplete': function(event, data){
                imagesUploaded = true;
            }
        });
        
    }
    
    
    //end ready//
});


function initialize(artist){

    var defaultLocation = new google.maps.LatLng(43.383343, -80.642867);
    var geoLocations = {
        "jason" : {
            'position' : new google.maps.LatLng(43.39105, -80.708716),
            'mailbox' : '/images/mailbox1.png'
        }, 
        "cheryl" : {
            'position' : new google.maps.LatLng(43.379357, -80.669524),
            'mailbox' : '/images/mailbox2.png'
        },
        "gloria" : {
            'position' : new google.maps.LatLng(43.381347, -80.664829),
            'mailbox' : '/images/mailbox3.png'
        },
        "rick" : {
            'position' : new google.maps.LatLng(43.407511,-80.642953),
            'mailbox' : '/images/mailbox4.png'
        },
        "christmas" : {
            'position' : new google.maps.LatLng(43.406528,-80.650506),
            'mailbox' : '/images/mailbox5.png'
        },
        "rosemary" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox6.png'
        },
        "mary" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox7.png'
        },
        "david" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox8.png'
        },
        "kevin" : {
            'position' : new google.maps.LatLng(43.349665,-80.533433),
            'mailbox' : '/images/mailbox9.png'
        },
        "annette" : {
            'position' : new google.maps.LatLng(43.346547, -80.535268),
            'mailbox' : '/images/mailbox10.png'
        },
        "marion" : {
            'position' : new google.maps.LatLng(43.345622, -80.534330),
            'mailbox' : '/images/mailbox11.png'
        },
        "jan" : {
            'position' : new google.maps.LatLng(43.336856, -80.545218),
            'mailbox' : '/images/mailbox12.png'
        },
        "martha" : {
            'position' : new google.maps.LatLng(43.327932, -80.587739),
            'mailbox' : '/images/mailbox13.png'
        },
        "anne" : {
            'position' :new google.maps.LatLng(43.327932, -80.587739),
            'mailbox' : '/images/mailbox14.png'
        }
    }
    
    
    
    
    
    
    var myOptions = {
        zoom: 15,
        center: defaultLocation,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new google.maps.Marker({
        position: defaultLocation,
        icon: '/images/mailbox0.png',
		clickable:false
    });
    
    // To add the marker to the map, call setMap();
    //marker.setMap(map);
    
    var infowindow = new google.maps.InfoWindow({
        maxWidth: 500
    });
    $("#address2").empty().load('/artists/addresses/' + artist + '.html', function(){
        infowindow.setContent($("#address2").html());
        marker.setPosition(geoLocations[artist].position);
        map.setCenter(geoLocations[artist].position);
        marker.setIcon(geoLocations[artist].mailbox);
		marker.setMap(map);
        infowindow.open(map, marker);
    });
    
    
    
    
}



function initializeAllMap(){
    var totalHeight = $(window).height() - ($("#footer_container").outerHeight())
    
    $("#map_canvas2").height(totalHeight);
    
    
    var defaultLocation = new google.maps.LatLng(43.364252,-80.618362);
    var geoLocations = {
        "jason" : {
			'position' : new google.maps.LatLng(43.39105, -80.708716),
			'mailbox' : '/images/mailbox1.png'
		}, 
        "cheryl" : {
			'position' : new google.maps.LatLng(43.379357, -80.669524),
            'mailbox' : '/images/mailbox2.png'
		},
		"gloria" : {
            'position' : new google.maps.LatLng(43.381347, -80.664829),
            'mailbox' : '/images/mailbox3.png'
        },
        "rick" : {
            'position' : new google.maps.LatLng(43.407511,-80.642953),
            'mailbox' : '/images/mailbox4.png'
        },
		"christmas" : {
            'position' : new google.maps.LatLng(43.406528,-80.650506),
            'mailbox' : '/images/mailbox5.png'
        },
        "rosemary" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox6-8.png'
        },
        "mary" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox6-8.png'
        },
        "david" : {
            'position' : new google.maps.LatLng(43.351791, -80.535805),
            'mailbox' : '/images/mailbox6-8.png'
        },
        "kevin" : {
            'position' : new google.maps.LatLng(43.349665,-80.533433),
            'mailbox' : '/images/mailbox9.png'
        },
        "annette" : {
            'position' : new google.maps.LatLng(43.346547, -80.535268),
            'mailbox' : '/images/mailbox10.png'
        },
		"marion" : {
            'position' : new google.maps.LatLng(43.345622, -80.534330),
            'mailbox' : '/images/mailbox11.png'
        },
		"jan" : {
            'position' : new google.maps.LatLng(43.336856, -80.545218),
            'mailbox' : '/images/mailbox12.png'
        },
		"martha" : {
            'position' : new google.maps.LatLng(43.327932, -80.587739),
            'mailbox' : '/images/mailbox13-14.png'
        },
		"anne" : {
            'position' :new google.maps.LatLng(43.327932, -80.587739),
            'mailbox' : '/images/mailbox13-14.png'
        }
    }
    
	
	var artistMarkers = [];
    var myOptions = {
        zoom: 12,
        center: defaultLocation,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
		scrollwheel: false
    };
    
    var map = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
    
    for (i in geoLocations) {
		
		var marker = new google.maps.Marker({
            position: geoLocations[i].position,
            icon: geoLocations[i].mailbox,
			clickable:false
        });
        marker.setMap(map)
        
    }
	$(".map").height(totalHeight);
    $('.map').jScrollPane();
}





function preloader(){
    var preloadImage = ['/images/trees.png', '/images/logo.png', '/images/mailbox.png'];
    
    for (i = 0; i < preloadImage.length; i++) {
        var tempImg = new Image();
        tempImg.src = preloadImage[i];
        
        $(tempImg).appendTo(".tempContainer");
    }
}


