function nospam(user,domain) {
    locationstring = "mailto:" + user + "@" + domain;
    window.location = locationstring;
}

function modal(link, modalWidth, modalHeight) {
	$("#modal").html("<iframe id='modalIframe' scrolling='no' frameborder='0' />");
	$("#modalIframe").attr("src", link);
	$('#modal').dialog({
					   autoOpen: false,
					   modal: true,
					   width: modalWidth,
					   position:'center'
					   });
	$('#modalIframe').css({"width": modalWidth+"px", "height": modalHeight+"px", "overflow": "hidden"});
	$('#modal').dialog('open');			
	$('#modal').bind('dialogclose', function(event, ui) {
        document.getElementById('modalIframe').contentWindow.pausePlayback();
	});
	
}
$(document).ready(function(){
	
	$("a.link-airports").click(function(){ $(".container").find("p").hide(); $("p.airports:hidden").show(); });
	$("a.link-banking").click(function(){ $(".container").find("p").hide(); $("p.banking:hidden").show(); });
	$("a.link-border").click(function(){ $(".container").find("p").hide(); $("p.border:hidden").show(); });
	$("a.link-casino").click(function(){ $(".container").find("p").hide(); $("p.casino:hidden").show(); });
	$("a.link-critical").click(function(){ $(".container").find("p").hide(); $("p.critical:hidden").show(); });
	$("a.link-residences").click(function(){ $(".container").find("p").hide(); $("p.residences:hidden").show(); });
	$("a.link-retail").click(function(){ $(".container").find("p").hide(); $("p.retail:hidden").show(); });
	$("a.link-industrial").click(function(){ $(".container").find("p").hide(); $("p.industrial:hidden").show(); });
	$("a.link-military").click(function(){ $(".container").find("p").hide(); $("p.military:hidden").show(); });
	$("a.link-port").click(function(){ $(".container").find("p").hide(); $("p.port:hidden").show(); });
	$("a.link-transportation").click(function(){ $(".container").find("p").hide(); $("p.transportation:hidden").show(); });
	$("a.link-law").click(function(){ $(".container").find("p").hide(); $("p.law:hidden").show(); });
	
	$("#home-awards li img").hover(
		function () {
			var offset = $(this).width();
			$(this).parent().children('.awardDescription').css({ display: "block", marginLeft: (offset-223)/2 }).animate({ opacity: 1, top: "125px" }, 200);
		},
		function () {
			$(this).parent().children('.awardDescription').animate({ opacity: 0, top: "140px" }, 300);
		}	
	);
	
	if(document.getElementsByTagName) {
			var a = document.getElementsByTagName("a");
			for(var i = 0; i < a.length; i++) {
				a[i].onfocus = function(){this.blur()};
				}
			}
	
	// Select the state after the country has been selected
	$('select#user_country').change(function() {
	  var country = 'country=' + $('select#user_country :selected').text();
	  $.get('/partners/users/update_state_select?' + country, function(data){
	    $("#addressStates").html( "<script type='text/javascript'>" + data + "</script>" );
	  })
	  return false;
	});
				
	// Modal content
	$(".modalLink").click(function(){
		var link = $(this).attr("href");
		var modalWidth = $(this).attr("width");
		var modalHeight = $(this).attr("height");
		modal(link, modalWidth, modalHeight);
		return false;
	});

	
	// Subnav Expand
	$(".expand").click(function(){
		$(this).next().slideToggle();
		$(this).parent().addClass("visible");
	});
	
		
	// Authentication for restricted content
	if ($.cookie("avigilon_authentication") != 1) {
		$(".auth a").click(function() {
			$('#authForm:not(.authToggle)').dialog({
												   autoOpen: false,
												   modal: true,
												   width: 555,
												   position: 'center'
												   });
			var name = $(this).attr("title");
			var link = $(this).attr("href");
			$.cookie('avigilon_document_request', link, { path: '/'}); // set cookie			
			$('#authFormTitle').html(name);
			$('#authForm').dialog('open');
			return false;
		});
		$("#skipSignup").click(function() {
			var link = $.cookie('avigilon_document_request');
			$.cookie('avigilon_authentication', 1, { path: '/'}); // set authenticated cookie
			window.open(link); // download requested document
			location.reload(true);
			$('#authForm').dialog('close');
		});
	}

	//if the user has filled a form for the content in question, show the content and hide the form
 	if ($.cookie('avigilon_authentication') != 1) {
		$("#authContent").html('<p>You do not have permission to view this page.  If you have permissions for this page, please log in to the Avigilon <a href="/partners">partners section</a>.</p>').css("visibility","visible");
	}
	else {
		$("#authContent").css("visibility","visible");
	}
	
	// hide protected content
	var authSpecial = $("#authContent").attr('class');
	if ($.cookie("avigilon_" + authSpecial) != 1) {
		$("#authContent." +authSpecial).html('<p>You do not have permission to view this page.  If you have permissions for this page, please log in to the Avigilon <a href="/partners">partners section</a>.</p>').css("visibility","visible");
		}
	else {
		$("#authContent." +authSpecial).css("visibility","visible");
	}

	//setup hash check for the made in canada video
	var tmp_hash = location.hash;
	if(tmp_hash.length > 0){
		if(tmp_hash.indexOf('made-in-canada') >= 0){
			modal('/flash/madeincanada.html','640','360');
		}
	}
		
});

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
/**
 * Create a cookie with the given name and value and other optional parameters.
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
