/* Custom Site JS File */
if(typeof(PUREXSAVESGREEN) === 'undefined') {
    var PUREXSAVESGREEN = {};
}

/* Document Ready Function */
$(document).ready(function(){
    //adds the invalid target attribute for 'external' links
    $('a.external').attr('target', '_blank');

    $('a#tell-a-friend').bind('click', function(event) {

        PUREXSAVESGREEN.openMyModal(event.target.href);
        return false;
    });

	$('#facebook-entry input').click(function(){Purex_FBConnect.login();return false;});
	
    $('#submit-post-contest-1').boxifyWithIframeLink();
	
	//contest entries ajax stuff
	$.ajaxSetup({cache: false});
	$('.months-list li a').click ( function () {
		$('.months-list li').removeClass('selected');
		$(this).parent().addClass('selected');
		$('#entries-wrapper').load($(this).attr('href'));
		return false;
	});
	$('ul.paginator li a').live('click', function() {
		$('#entries-wrapper').load($(this).attr('href'));
		return false;
	});
	$().ajaxStart(function () {
		$('#entries-wrapper').html('<p id="ajax-loader"><img src="/img/contest-entries/ajax-loader.gif" alt="Loading" /></p>');
	});
	
	//end contest entries
	
	$('#save_green_callout').boxifyWithIframeLink({width:675, height:420});
});

//Close modal window on sharing submit of clicking on link to home pages
PUREXSAVESGREEN.closeSharing ={

    };

PUREXSAVESGREEN.modalWindow = {
    parent : "body",
    windowId : null,
    content : null,
    width : null,
    height : null,
    close : function() {
        $(".modal-window").remove();
        $(".modal-overlay").remove();
    },
    open : function() {
        var modal = "";
        modal += "<div class=\"modal-overlay\"></div>";
        modal += "<div id=\"" + this.windowId
        + "\" class=\"modal-window\" style=\"width:" + this.width
        + "px; height:" + this.height + "px; margin-top:-"
        + (this.height / 2) + "px; margin-left:-"
        + (this.width / 2) + "px;\">";
        modal += this.content;
        modal += "</div>";

        $(this.parent).append(modal);

        $(".modal-window").append("<a class=\"close-window\"></a>");
        $(".close-window").click(function() {
            PUREXSAVESGREEN.modalWindow.close();
        });
        $(".modal-overlay").click(function() {
            PUREXSAVESGREEN.modalWindow.close();
        });
        $().bind("keypress", function(event) {
            if (27 == event.keyCode) {
                PUREXSAVESGREEN.modalWindow.close();
            }
        });
    }
}

PUREXSAVESGREEN.openMyModal = function(siteUrl) {
    source = siteUrl + '?modal=true';
    PUREXSAVESGREEN.modalWindow.windowId = "myModal";
    PUREXSAVESGREEN.modalWindow.width = 570;
    PUREXSAVESGREEN.modalWindow.height = 600;
    PUREXSAVESGREEN.modalWindow.content = "<iframe width='570' height='600' frameborder='0' scrolling='no' allowtransparency='true' src='"
    + source + "'></iframe>";
    PUREXSAVESGREEN.modalWindow.open();
}

/**
 * plugin for wiring up ajax modal windows using the Boxy plugin
 * pass in a function to afterShow to execute a callback after the modal is opened
 *
 */

$.fn.boxifyLink = function(options) {
	var settings = jQuery.extend({
		afterShow: function(){
			$('.boxy-wrapper .form-cancel').show().click(function(){
				Boxy.get(this).hide();
				return false;
			})
		}
	}, options);

	$(this).click(function(){
		Boxy.load($(this).attr('href'), {
			modal: true,
			afterShow: settings.afterShow,
			title: " "
		});
		return false;
	});

};
$.fn.boxifyWithIframeLink = function(options) {
	var settings = jQuery.extend({
		afterShow: function(){
			$('.boxy-wrapper .form-cancel').show().click(function(){
				Boxy.get(this).hide();
				return false;
			})
		},
		width: 353,
		height: 440
		}
	, options);

	$(this).click(function(){
		var iframe = $('<iframe src="' + $(this).attr('href') + '"  width="'+ settings.width +'" height="'+ settings.height +'" frameborder="0" scrolling="no" style="border:none;"></iframe>');
		document.currentBoxyIframe = iframe;
		document.currentBoxy = new Boxy (iframe, { 
			unloadOnHide: true, 
			draggable: false, 
			show: true, 
			modal: true,
			afterShow: settings.afterShow,
			title: " "
		})
		return false;
	});

};

