$(function() {

    var itemWrap = $('.item-wrap');
    if (itemWrap.length) {
	var lastItemWrap = $(itemWrap[itemWrap.length-1]);
	lastItemWrap.css({'border':'0'});
    }

	$('input[@value="Continue Shopping"]').after('<input type="image" alt="continue shopping" src="images/buttons/btn-continue.jpg" id="btn-continue" name="view" />').remove();
	$('input[@value="Checkout"]').after('<input type="image" alt="checkout" src="images/buttons/btn-checkout.jpg" id="btn-checkout" name="checkout" />').remove();
	$('input[@value="Save Address"]').after('<input type="image" alt="Save Address" src="images/buttons/save-address.gif" id="btn-address" name="submit" />').remove();
	$('input[@value="Cancel"]').after('<input type="image" alt="Cancel" src="https://citizenm.esitetoolbox.com/images/buttons/cancel.gif" id="btn-cancel" name="cancel" value="Cancel"  />').remove();
	$('input[@value="Update Account"]').after('<input type="image" src="images/buttons/update-account.gif" alt="Update Account" name="submit" />').remove();
	$('input[@value="Edit Cart"]').after('<input type="image" src="images/buttons/edit-cart.gif" alt="Edit Cart" name="editcart" id="edit-cart" />').remove();
	$('input[@value="Continue to Address Information"]').after('<input type="image" src="images/buttons/continue.gif" alt="Continue to address information" name="checkout" />').remove();
	$('input[@value="Continue to Shipping"]').after('<input type="image" src="images/buttons/shipping.gif" alt="Continue to Shipping" name="checkout" />').remove();
	$('input[@value="Proceed to Address Information"]').after('<input type="image" src="images/buttons/proceed.gif" alt="Proceed to Address Information" name="submit" />').remove();
	$('input[@value="Proceed to Shipping"]').after('<input type="image" src="images/buttons/proceed-shipping.gif" alt="Proceed to Shipping" name="submit" />').remove();
	$('input[@value="Log In"]').after('<input type="image" src="images/buttons/login.gif" alt="Login" name="LogIn" />').remove();
	$('input[@value="Quick Checkout"]').after('<input type="image" src="images/buttons/quick-checkout.gif" alt="Quick Checkout" name="submit" />').remove();
	$('input[@value="Send Password"]').after('<input type="image" src="images/buttons/send.gif" alt="Send Password" name="sendpass" id="send-password" />').remove();
	$('input[@value="Review Order"]').after('<input type="image" src="images/buttons/review-order.gif" alt="Review Order" name="checkout" id="review-order-btn" />').remove();
	$('input[@value="Yes, I confirm my order"]').after('<input type="image" src="https://citizenm.esitetoolbox.com/images/buttons/confirm.gif" alt="yes, I confirm my order" name="payment" id="submit3" />').remove();

	$('#btn-continue').click(function() {
	    window.location.href='index.php?op=view#continue'
	    return false;
	});

	$('#btn-cancel').click(function() {
	    window.opener.location.reload();window.close();
	});

	$('#edit-cart').click(function() {
	    window.location.href = 'index.php?op=viewcart';
	});

	$('input[type=hidden]').hide();

	// $('#check-availability-wrapper').tabs({ fxAutoHeight: true });
	
	$('div#content-wrapper #title-wrapper, div#headers h2').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );
	$('body.home div#content-wrapper h2').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff;', strWmode: 'transparent' }, { expressInstall: true } );

	
	$('div#content h1, div#TB_caption').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );
	$('div#content h2').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#E2003D', strLinkColor: '#E2003D', strHoverColor: '#E2003D', strWmode: 'transparent' }, { expressInstall: true } );
	$('div#content h2#map-bottom').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#E2003D', strLinkColor: '#E2003D', strHoverColor: '#E2003D', strWmode: 'transparent', strFlashVars: 'textalign=right&' }, { expressInstall: true } );
	$('div#login-welcome h4').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );



// Input Elements that need a calendar popup, but no calendar Icon, need a class of "makeitpopup"
	$(".makeitpopup").click(function() {
		cal.select( this, this.id, "MM/dd/yyyy" );
		return false;
	});


// Finds all elements with a class of "form-cal" and replaces them with a calendar icon, link, and onclick handler
// Only difference between euro and regular formats is the default values in boxes, formatDate() does not need to change
	$(this).find(".form-cal-euro").each(function(i) {
		var icon = '<a href="#" class="icon-cal" id="form-cal-euro-' + i + '" name="form-cal-euro-' + i + '" title="Click to select a date"><img src="images/icon-cal.gif" /></a>';
		$(icon).appendTo($(this).parent()).click(function(){
			//hide all dropdowns for IE6 and lower
			if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","hidden"); }

			var o = this.previousSibling;
			while (o.nodeType != 1) { o = o.previousSibling; }
			
			// check to see if it is an arrival/departure pair
			if($(o).is(".date-end")) {
				var arrive_string = $(o).parent().prev(".field").children(".date-begin").attr("value");
				var arrive_label = $(o).parent().prev(".field").children("label").html().replace(":","");
				//check to see if begin date has been set already and adjust disabled/current dates
				if (arrive_string != "dd/mm/yyyy" && arrive_string) {
					var arrive_array = arrive_string.split('/');
					var arrive_date = new Date(arrive_array[1]+'/'+arrive_array[0]+'/'+arrive_array[2]);
					var arrive_date = formatDate(arrive_date, "MM/dd/yyyy" );
					cal.clearDisabledDates();
					cal.addDisabledDates( null, arrive_date );
					cal.select(o,$(this).attr("id"),"dd/MM/yyyy", arrive_date);
					return false;
				}
				// begin date not set first, so pull label text and alert
				else {
					alert("Please select \"" + arrive_label + "\" first");
					if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","visible"); }
					return false;
				}
			}
			
			if($(o).is("#arrive")) {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setFullYear(2008, 5, 25);
				cal.addDisabledDates( null, formatDate( yesterday, "MM/dd/yyyy" ));
				yesterday.setDate( yesterday.getDate() + 1 );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", formatDate( yesterday, "dd/MM/yyyy" ) );
				return false;
			}

			if($(o).is(".archive-begin")) {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() + 1 );
				cal.addDisabledDates( formatDate( yesterday, "MM/dd/yyyy" ), null );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", yesterday );
				return false;
			}

			//searching dates in the past
			if($(o).is(".archive-end")) {
				var arrive_string = $(o).parent().prev(".field").children(".archive-begin").attr("value");
				var arrive_label = $(o).parent().prev(".field").children("label").html();
				//check to see if begin date has been set already and adjust disabled/current dates
				if (arrive_string != "dd/mm/yyyy" && arrive_string) {
					var arrive_array = arrive_string.split('/')
					var arrive_date = new Date(arrive_array[1]+'/'+arrive_array[0]+'/'+arrive_array[2]);
					var arrive_date = formatDate(arrive_date, "dd/MM/yyyy" );
					cal.clearDisabledDates();
					var yesterday = new Date();
					yesterday.setDate( yesterday.getDate() + 1 );
					cal.addDisabledDates( null, arrive_date );
					cal.addDisabledDates( formatDate( yesterday, "dd/MM/yyyy" ), null );
					cal.select(o,$(this).attr("id"),"dd/MM/yyyy", arrive_date);
					return false;
				}
				// begin date not set first, so pull label text and alert
				else {
					alert("Please select \"" + arrive_label + "\" first");
					if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","visible"); }
					return false;
				}
			}
			// it's just a plain calendar popup
			else {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() - 1 );
				cal.addDisabledDates( null, formatDate( yesterday, "dd/MM/yyyy" ) );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", yesterday );
				return false;
			}
		});
		$(this).remove();
	});

// Sample sifr usage - remove if not needed
	/*
	// intPadding array order is: left, top, right, bottom - must match css for corresponding element
	$('div#content h2').sifr(
		{ strSWF: 'flash/font-name.swf', strColor: '#cc0000', strLinkColor: '#cccc00', strHoverColor: '#cc00cc', strWmode: 'transparent', intPadding: [35, 0, 0, 0], strFlashVars: 'textalign=center&', strCase: 'upper' },
		{ expressInstall: true }
	);
	*/


// Sample usage of jquery.flash plugin - remove if not needed
	/*	If given the following markup:

	<div id="masthead">
		<img src="images/mastheads/flash-alt.jpg" alt="" /><br />
	</div>
	
	you would use the following to replace the alternate content:

	$("div#masthead").flash(
		{src:"flash/home.swf",
		 width:600,
		 height:300,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"});
	*/

	$('.converter').click(function() {
		var price = $(this).prev('.price').length ? $(this).prev().text().slice(1) : 1;
		window.open( 'converter.php?price=' + price, '', 'width=704,height=400,resizable=yes');
		return false;
	});

});
