$(function() {

	$('div#content h1').sifr({ strSWF: '/fonts/helvetica.swf', strColor: '#b7351a', strWmode: 'transparent', strCase: 'upper' },{ expressInstall: true });
	$('div#content h2').sifr({ strSWF: '/fonts/helvetica.swf', strColor: '#443424', strWmode: 'transparent' },{ expressInstall: true });
	$('div#tour-search h3').sifr({ strSWF: '/fonts/helvetica.swf', strColor: '#443424', strWmode: 'transparent', strFlashVars: 'textalign=center&' },{ expressInstall: true });
	$('div#content h3#excursion-header').sifr({ strSWF: '/fonts/helvetica.swf', strColor: '#443424', strWmode: 'transparent' },{ expressInstall: true });
	$('div#results-content h5').sifr({ strSWF: '/fonts/helvetica-bold.swf', strColor: '#ffffff', strWmode: 'transparent' },{ expressInstall: true });
	$('.package-title').sifr({ strSWF: '/fonts/helvetica.swf', strColor: '#CA3418', strWmode: 'transparent' },{ expressInstall: true });

	//$('#package-tab').tabs();
	
	$(".pickdate").each(function() {
		$(this).datepicker({dateFormat: "mm/dd/yy", duration: "", hideIfNoPrevNext: true, beforeShow: setDatePicker });
		if($(this).val()=='mm/dd/yyyy') $(this).val('');
	}).attr("readonly", "readonly").addClass('readonly');

	$("body.home #masthead").flash(
		{src:"/flash/masthead-slideshow.swf",
		 width:661,
		 height:368,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"},
			function(htmlOptions) { //Use this to specify a query string, take out if not needed.
				htmlOptions.flashvars.lang = LANG;
				htmlOptions.flashvars.xmlfile = "/"+LANG+"/flash/xml/home.xml";
				$(this).html($.fn.flash.transform(htmlOptions));
		});
        
    $("body.summer-landing #inset").flash(
        {src:"/flash/rotating-inset.swf",
         width:241,
         height:194,
         wmode:"transparent",
         quality:"high"},
         {version:"9"},
            function(htmlOptions) { //Use this to specify a query string, take out if not needed.
                htmlOptions.flashvars.lang = LANG;
                htmlOptions.flashvars.xmlFile = "/flash/xml/thrill-inset.xml";
                $(this).html($.fn.flash.transform(htmlOptions));
        });
	
	$('#push-experience-home').each(function() {
		var nav = $('#home-carousel-wrapper ul', this);
		var num = nav.children().length
		nav.width(nav.children().width()*num);
		
		$.each(nav.children(), function(i) {
			$('#control-num').append('<li><a href="#">'+(i+1)+'</a></li>');							
		});
									 
		$(this).serialScroll({
			target:'#home-carousel-wrapper',
			items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
			prev:'#btn-prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
			next:'#btn-next',// Selector to the 'next' button (absolute too)
			axis:'x',// The default is 'y' scroll on both ways
			navigation:'#control-num li a',
			force: true,
			duration:400,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
			lock: false,
			stop: true,
			interval:6000, // It's the number of milliseconds to automatically go to the next
			onBefore:function( ){
				$('#control-num li.current').removeClass('current');
			},
			onAfter:function( elem ){
				var index = $('#home-carousel-wrapper ul li').index(elem);
				$('#control-num li').eq(index).addClass('current');
			}
		});
	});


	$('#tools-inner').each(function( i, obj ) {
	    var ul = $( obj ).find( 'ul' );

	    ul.width( ul.children().width() * ul.children().length );

	    $( obj ) .serialScroll({
		target: 	'#tools-content',
		items: 		'li',
		next: 		'#next-tools',
		axis: 		'x',
		force: 		true,
		duration: 	400,
		lock: 		false,
		stop: 		true,
		interval: 	6000
	    })
	});

	$('#experience-inner').each(function() {
		var nav = $('#experience-content ul', this);
		var num = nav.children().length
		nav.width(nav.children().width()*num);	
		
		$(this).serialScroll({
			target:'#experience-content',
			items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
			next:'#btn-next',// Selector to the 'next' button (absolute too)
			axis:'x',// The default is 'y' scroll on both ways
			force: true,
			duration:400,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
			lock: false,
			stop: true,
			interval:6000 // It's the number of milliseconds to automatically go to the next
		});
	});
	
	$('a.videoBox').each(function() { // run in the execution context of each .videoBox anchor element
	   	var link = this.href; // set a reference to the href attribute value
		    var iContainer = $('<div />').addClass('iframeWrapper')
			.html('<iframe src="'+link+'" class="videoIframe" frameborder="0"></iframe>')
			.jqm({ trigger:'div#video-content a.videoBox' })
			.addClass('jqmWindow')
			.append('<p class="closeBox"><a href="#">close</a> this video</p>');
			
		$(this).click(function() {
		    $('body').append(iContainer);
		    iContainer.jqmShow().jqmAddClose( $('.closeBox a') );
		    return false;
		});
	});
	
	$('a#forecast-link').click(function() { window.open("/"+LANG+"/popup-weather.php","weather","menubar=0,resizable=0,width=653,height=583" ); return false; });
	$('a#imap-link').click(function() { window.open('http://www.gotopuertorico.com/imap/popmap.php','popupWindow','menubar=yes,scrollbars=yes,resizable=no,width=770,height=560'); return false; });
	$('li#secondary-plan-interactive a').click(function() { window.open('http://www.gotopuertorico.com/imap/popmap.php','popupWindow','menubar=yes,scrollbars=yes,resizable=no,width=770,height=560'); return false; });

	$("div.gallery").each( function() {
		var file = $(this).attr("id");
		$(this).flash({	
			src:"/flash/photo_gallery.swf",
			width:600,
			height:420,
			wmode:"transparent",
			quality:"high"},
			{version:"8"},
			function(htmlOptions) {
				//htmlOptions.flashvars.configFile = '/flash/xml/gallery_config.xml';
				htmlOptions.flashvars.xmlFile = "/flash/xml/"+file+".xml";
				$(this).html($.fn.flash.transform(htmlOptions));				
			}
		);
	});
	
	$('body.home').each(function() {
		var url = window.location.search.substring(1), query;
		if(url.length>0) {
			query = parseQuery(url);
			if(query.error_message!==null&&query.error_message!==undefined) {
				alert(query.error_message.replace(/\+/g,' '));
			}
		}
	});
});

function parseQuery(str) {
	var vars = str.split("&"),
		pairs = {}, p;
		
	for (var i=0;i<vars.length;i++) {
		p = vars[i].split("=");
		pairs[p[0]] = this.urlDecode(p[1]);
	}
	return pairs;
}

function urlDecode(encodedString) {
	var output = encodedString;
	var binVal, thisString;
	var myregexp = /(%[^%]{2})/;
	while ((match = myregexp.exec(output)) !== null && match.length > 1 && match[1] !== '') {
		binVal = parseInt(match[1].substr(1),16);
		thisString = String.fromCharCode(binVal);
		output = output.replace(match[1], thisString);
	}
	return output;
}
