$(function() {
	centerNavigation();
	if($('ul#home-Images li').length > 1) {
		var img = $('ul#home-Images li.first img');
		if(img[0].complete) {
			$('ul#home-Images').innerfade({speed: 1200, timeout: 3000, type: 'sequence'});
		} else {
			img.load(function() {
				$('ul#home-Images').innerfade({speed: 1200, timeout: 3000, type: 'sequence'});
			});
		}
	}
	pagination.init();
	//deObfuscateEmail.init();
	if($('div#mediaPlayer').length > 0) {setFlash();}
});

function centerNavigation() {
	$('ul#topMenu li ul, ul#navigation li ul').each(function() {
		var left = $(this).width() / 2 + 1;
		$(this).css('left','50%').css('margin-left', '-'+left+'px').css('right','auto');
	});
}

var pagination = {
	page : 1,
	maxPages : '',
	contentWidth : 430,
	prevButton : $('a#prevButton'),
	nextButton : $('a#nextButton'),
		
	init: function() {
		this.maxPages = parseInt($('div#inner-ProductContent').css('width')) / this.contentWidth;
	
		if($('div#pagination').length > 0) {
			$('div#pagination select').change(function() {
				pagination.page = this.value;
				pagination.slideProducts();
			});
			
			$('div#pagination a').click(function() {
				var type = $(this).attr('id');
				
				if(type == 'nextButton' && pagination.page < pagination.maxPages) {pagination.page++;}
				else if(type == 'prevButton' && pagination.page > 0) {pagination.page--;}
				pagination.slideProducts();
				return false;
			});
		}
		
		$('div#inner-ProductContent a').click(function() {
			var details = $(this).attr('rel').split('|||');
			var productContent = $('div#productContent');
			$('img',productContent).attr('src',details[0]);
			$('h2',productContent).html(details[1]);
			$('span#productModel',productContent).html(details[2]);
			$('div#productDescription',productContent).html(details[3]);
			$('h2, p, div, strong',productContent).css('display','block');
			return false;
		});
		
		$('p#loadingProductListing').remove();
		$('div#productListing').css('display','block');
	},

	slideProducts : function() {
		if(this.page == 1) {$('a#prevButton').css('visibility','hidden');}
		else {$('a#prevButton').css('visibility','visible');}
		
		if(this.page == this.maxPages) {$('a#nextButton').css('visibility','hidden');}
		else {$('a#nextButton').css('visibility','visible');}
		
		$('div#pagination select option:eq('+(this.page-1)+')').attr('selected','selected');
		
		$('div#inner-ProductContent').animate({marginLeft : '-'+((this.page-1)*this.contentWidth)+'px'}, 750);
	}
}

var deObfuscateEmail = {
	init: function() {
		var emails = new Array();
		$('div#ProductBackground a').each(function() {
			var em = $(this).attr('href');
			if(em.indexOf('mailto:') != -1) {
				em = em.substr(7);
				if(em.indexOf('?subject=') != -1) {
					var em2 = em.split('?subject=');
					em = deObfuscateEmail.ec13(em2[0])+'?subject='+em2[1];
				} else {em = deObfuscateEmail.ec13(em);}
				$(this).attr('href','mailto:'+em);
			}
		});
	},
	
	ec13: function(s) {
		var b=[],c,i=s.length,
		a='a'.charCodeAt(),z=a+26,
		A='A'.charCodeAt(),Z=A+26;
		while(i--){
			c=s.charCodeAt(i);
			if(c>=a&&c<z){b[i]=deObfuscateEmail.ec(c,a,13);}
			else if(c>=A&&c<Z){b[i]=deObfuscateEmail.ec(c,A,13);}
			else{b[i]=s.charAt(i);}
		}
		return b.join( '' );
	},
	
	ec: function(t,u,v) {
		return String.fromCharCode(((t-u+v)%(v*2))+u);
	}
}

function setFlash() {
	$('div#mediaPlayer').flash({
		swf: 'http://video.getmarried.com/templates/V0-400x225chromeless.swf',
		width: '400',
		height: '255',
		allowFullScreen: 'false',
		allowScriptAccess: 'always',
		wmode: 'transparent',
		hasVersion: '9',
	    flashvars: {
			ts: '1285598278',
			projectid: '72352',
			projectuuid: 'jjwmys45',
			programid: '',
			programuuid: '',
			loadingdomain: 'http://video.getmarried.com',
			serviceURL: 'http://video.getmarried.com/services/index.php',
			doResize: 'false',
			playerType: 'vod'
		}
	});
}


function updateDatePullDownMenu(objForm, fieldName) {
  var pdmDays = fieldName + "_days";
  var pdmMonths = fieldName + "_months";
  var pdmYears = fieldName + "_years";

  time = new Date(objForm[pdmYears].options[objForm[pdmYears].selectedIndex].text, objForm[pdmMonths].options[objForm[pdmMonths].selectedIndex].value, 1);

  time = new Date(time - 86400000);

  var selectedDay = objForm[pdmDays].options[objForm[pdmDays].selectedIndex].text;
  var daysInMonth = time.getDate();

  for (var i=0; i<objForm[pdmDays].length; i++) {
    objForm[pdmDays].options[0] = null;
  }

  for (var i=0; i<daysInMonth; i++) {
    objForm[pdmDays].options[i] = new Option(i+1);
  }

  if (selectedDay <= daysInMonth) {
    objForm[pdmDays].options[selectedDay-1].selected = true;
  } else {
    objForm[pdmDays].options[daysInMonth-1].selected = true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function checkBox(object) {
  document.account_newsletter.elements[object].checked = !document.account_newsletter.elements[object].checked;
}

function popupWindow(url, name, params) {
  window.open(url, name, params).focus();
}

//jQuery SWFObject v1.1.1 MIT/GPL @jon_neal
//http://jquery.thewikies.com/swfobject
(function(f,h,i){function k(a,c){var b=(a[0]||0)-(c[0]||0);return b>0||!b&&a.length>0&&k(a.slice(1),c.slice(1))}function l(a){if(typeof a!=g)return a;var c=[],b="";for(var d in a){b=typeof a[d]==g?l(a[d]):[d,m?encodeURI(a[d]):a[d]].join("=");c.push(b)}return c.join("&")}function n(a){var c=[];for(var b in a)a[b]&&c.push([b,'="',a[b],'"'].join(""));return c.join(" ")}function o(a){var c=[];for(var b in a)c.push(['<param name="',b,'" value="',l(a[b]),'" />'].join(""));return c.join("")}var g="object",m=true;try{var j=i.description||function(){return(new i("ShockwaveFlash.ShockwaveFlash")).GetVariable("$version")}()}catch(p){j="Unavailable"}var e=j.match(/\d+/g)||[0];f[h]={available:e[0]>0,activeX:i&&!i.name,version:{original:j,array:e,string:e.join("."),major:parseInt(e[0],10)||0,minor:parseInt(e[1],10)||0,release:parseInt(e[2],10)||0},hasVersion:function(a){a=/string|number/.test(typeof a)?a.toString().split("."):/object/.test(typeof a)?[a.major,a.minor]:a||[0,0];return k(e,a)},encodeParams:true,expressInstall:"expressInstall.swf",expressInstallIsActive:false,create:function(a){if(!a.swf||this.expressInstallIsActive||!this.available&&!a.hasVersionFail)return false;if(!this.hasVersion(a.hasVersion||1)){this.expressInstallIsActive=true;if(typeof a.hasVersionFail=="function")if(!a.hasVersionFail.apply(a))return false;a={swf:a.expressInstall||this.expressInstall,height:137,width:214,flashvars:{MMredirectURL:location.href,MMplayerType:this.activeX?"ActiveX":"PlugIn",MMdoctitle:document.title.slice(0,47)+" - Flash Player Installation"}}}attrs={data:a.swf,type:"application/x-shockwave-flash",id:a.id||"flash_"+Math.floor(Math.random()*999999999),width:a.width||320,height:a.height||180,style:a.style||""};m=typeof a.useEncode!=="undefined"?a.useEncode:this.encodeParams;a.movie=a.swf;a.wmode=a.wmode||"opaque";delete a.fallback;delete a.hasVersion;delete a.hasVersionFail;delete a.height;delete a.id;delete a.swf;delete a.useEncode;delete a.width;var c=document.createElement("div");c.innerHTML=["<object ",n(attrs),">",o(a),"</object>"].join("");return c.firstChild}};f.fn[h]=function(a){var c=this.find(g).andSelf().filter(g);/string|object/.test(typeof a)&&this.each(function(){var b=f(this),d;a=typeof a==g?a:{swf:a};a.fallback=this;if(d=f[h].create(a)){b.children().remove();b.html(d)}});typeof a=="function"&&c.each(function(){var b=this;b.jsInteractionTimeoutMs=b.jsInteractionTimeoutMs||0;if(b.jsInteractionTimeoutMs<660)b.clientWidth||b.clientHeight?a.call(b):setTimeout(function(){f(b)[h](a)},b.jsInteractionTimeoutMs+66)});return c}})(jQuery,"flash",navigator.plugins["Shockwave Flash"]||window.ActiveXObject);

