/// <reference path="../../jquery/jquery-1.4.2.js" />
/// <reference path="../../jquery/jquery-1.4.2-vsdoc.js" />

//  Stephën Miessner
/*var $j = jQuery.noConflict();*/
$(document).ready(function()
{
    $.fn.search = function() 
    {
	    return this.focus(function() {
	    if( this.value == this.defaultValue ) {
		    this.value = "";
	    }
	    }).blur(function() {
		    if( !this.value.length ) {
			    this.value = this.defaultValue;
		    }
	    });
    };
    $(".textinput").search();	
	
	

	

	/*	add even and odd class for styling
	--------------------------------------------------------------------*/
	// if ($('.fastFacts').length) { 
	//	don't do below line - it takes into account all concurrent p tags not on a per div basis
			//$('.fastFacts p:even').addClass('first');
	//	instead do this to start over at each new selector div
	$('.fastFacts p:nth-child(odd)').addClass('odd');
	$('.fastFacts p:nth-child(even)').addClass('even');
	$('.fastFacts p:nth-child(1)').addClass('first');
	
	
	
	/*	add class to datatable headers
	--------------------------------------------------------------------*/
	
	if($('body.cvbcompare #scResultGrid').length){	
		//alert('length');
		//	For Featured destinations
			//show first header (<th> row)
		$('tr#featuredHeader:first').addClass('first');
			//	show every 10th table header row
		$("tr#featuredHeader:nth-child(5n+1)").addClass('fifth');
		//	For NON-Featured destinations
			//show first header (<th> row)
		$('tr#nonFeaturedHeader:first').addClass('first');
			//	show every 10th table header row
		$("tr#nonFeaturedHeader:nth-child(5n+1)").addClass('fifth');		

		
	}




	
	
	//	TEXT REPLACEMENT
	
	$("#destTitle span.titleCityState:contains('Other'), #destFastFacts span.address:contains('Other'), #ctl00_Main_rptFeaturedResults_ctl00_pnlMoreData:contains('Other')").each(function(){
        $(this).text($(this).text().replace(/Other/, "Canada"));
	});
	
	
	/*	Add commas to numbers (0,000)s	
	-------------------------------------------------------------------*/
	//#http://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits-using-jquery#1990590
	
    /*
	$.fn.digits = function(){ 
		return this.each(function(){ 
			$(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
		})
	}
	$("span.factStat").digits();
    */

	
	/*	=Thumbnail Picker
	-------------------------------------------------------------------*/
	if($('body.cvb #photos').length){
		$("#large_images li").each(function(index, element){$(element).attr("class", 'hide');});
		$("#large_images li").each(function(index, element){$(element).attr("id", 'img'+index);});
		$("#thumb_holder li a").each(function(index, element){$(element).attr("rel", 'img'+index);});
		
		var mainImg ='img0';
		var current = 'img0';
		
		$('#img0').css('display', 'inline');
		$('#img0').addClass('current');
		
		$('#thumb_holder li a').click (function(){								   
			mainImg = $(this).attr('rel');
			if(mainImg != current){
				$('.current').fadeOut('slow');
				$('#'+mainImg).fadeIn('slow', function(){
					$(this).addClass('current');
					current = mainImg;
				});
			}
		});	
	}//	end Thumbnail Picker
		


		/* = RANDOM AND ROTATING QUOTES	
		-------------------------------------------------*/

		if($('#testimonials .singlequote, #TESTIMONIALS .singlequote').length > 1){
				//$('.singlequote').hide();
				var pause = 10000; // define the pause for each tip (in milliseconds)
				var countedItems = $('.singlequote').length;
				var itemId = 1;
				var testimonialNumber = Math.floor(Math.random()*countedItems+1);
				$('.singlequote').each(function(){
					$(this).attr('id', itemId);
					itemId++;
				});
				function randomTestimonial() {
					visibleTestimonial = $('.singlequote:visible').attr('id');
					while(visibleTestimonial == testimonialNumber){
						testimonialNumber = Math.floor(Math.random()*countedItems+1);
					}
					$('.singlequote').hide();
					$('#' + testimonialNumber).fadeIn(2500);
					var me = arguments.callee;	//
					//console.log('Callee: ',arguments.callee)	//	callee: randomTestimonial()
					//console.log('Caller: ',arguments.caller);	//	caller:	undefined
		
					var timer = setTimeout(function() { me(); }, pause);
				}
				/* = RANDOM AND ROTATING QUOTES	 (ACTIVATE)
			    -------------------------------------------------*/
				randomTestimonial();	
		}//	end randomTestimonial	
/*
	var whichToShow = Math.floor(Math.random() * $('.singlequote').length);
	$('.singlequote').hide();	
	$('.singlequote').hide().eq(whichToShow).fadeIn(1000);
*/	



				/* = hide right col if no testimonials
			    -------------------------------------------------*/
if($('body.services').length){
	if ($('body.services #testimonials').length < 1) { 
		//alert('you get no right column');
		$('.twothirds').addClass(' threethirds');
		$('.onethirds #followup, .onethirds #pressreleases').remove();
	}
}

		
/*	=EQUALIZE COLUMN HEIGHT
-------------------------------------------------*/
function equalHeight(group) {
  tallest = 0;
  group.each(function() {
	thisHeight = $(this).height();
	if(thisHeight > tallest) {
	  tallest = thisHeight;
	}
  });
  group.height(tallest);
}	

				/* = EQUALIZE COLUMN HEIGHT	 (ACTIVATE)
			    -------------------------------------------------*/
				//equalHeight($(".hmpgGeneral"));
				equalHeight($("body.services #services, .onethirds"));	

if ($('body.default_newaug11, body.default').length) { 
    //equalHeight($("#destFinderHome, #seoCopy"));
	//equalHeight($("#leftCol, #seoCopy"));
}



if ($('body.services #androidApp, body.cvb #androidApp').length) { 

	var htmllink = $('a#androidAppLink');
  	var marketurl = htmllink.attr('href');
 	 //var htmllinktext = $(htmllink).text();
  	var htmllinktext = $('a#androidAppLink span');
  
  	//alert(htmllinktext);
	



  
  	//var marketurl = 'https://market.android.com/details?id=com.mobilaurus.visitDenver';

  	$.get(marketurl, function(res) { //get the html source of this website
    	$(res.responseText).find('.doc-banner-icon').each(function() { //loop though all h3 in the snippets list
      		var image = $(this).children('img:last'); //get the image
	  		var imagesrc = image.attr('src');
	  		var theimage = $('<img src="'+ imagesrc +'" />');
	  		//htmllinktext.replaceWith(theimage);
      		//var htmllink = $('<a href='+marketurl+'/>');
	  		//$('a#androidAppLink span').replaceWith(theimage);

  
	  		htmllinktext.html(jQuery('<img/>', { //with an img element inside it
             	src: image.attr('src'), //set the href for the link
				//text: anchor.text() //and the text
			}));
			
			htmllinktext.addClass('killDefault'); //remove default settings to use default icon
			  
	  
      		/*jQuery('<span/>', { //build a li element
				html: jQuery('<img/>', { //with an img element inside it
					src: image.attr('src'), //set the href for the link
					//text: anchor.text() //and the text
				})
      		}).replaceAll($('a#androidAppLink span')); //append it to a list*/
		});
      
    //$('#androidApp li:first').remove(); //remove this first li ("Loading...") when done
	});
}	
	
	
	if ($('body.services #iosApp, body.cvb #iosApp').length) { 

	var iOShtmllink = $('a#iosAppLink');
  	var iOSmarketurl = iOShtmllink.attr('href');
 	 //var htmllinktext = $(htmllink).text();
  	var iOShtmllinktext = $('a#iosAppLink span');
  
  	//alert(htmllinktext);
	



  
  	//var marketurl = 'https://market.android.com/details?id=com.mobilaurus.visitDenver';

  	$.get(iOSmarketurl, function(res) { //get the html source of this website
    	$(res.responseText).find('#left-stack div.artwork:first').each(function() { //loop though all h3 in the snippets list
      		var iOSimage = $(this).children('img.artwork:first'); //get the image
	  		var iOSimagesrc = iOSimage.attr('src');
			//alert(iOSimagesrc);			
	  		var iOStheimage = $('<img src="'+ iOSimagesrc +'" />');
	  		//htmllinktext.replaceWith(theimage);
      		//var htmllink = $('<a href='+marketurl+'/>');
	  		//$('a#androidAppLink span').replaceWith(theimage);

  
	  		iOShtmllinktext.html(jQuery('<img/>', { //with an img element inside it
             	src: iOSimage.attr('src'), //set the href for the link
				//text: anchor.text() //and the text
			}));
			
			iOShtmllinktext.addClass('killDefault'); //remove default settings to use default icon


			  
	  
      		/*jQuery('<span/>', { //build a li element
				html: jQuery('<img/>', { //with an img element inside it
					src: image.attr('src'), //set the href for the link
					//text: anchor.text() //and the text
				})
      		}).replaceAll($('a#androidAppLink span')); //append it to a list*/
		});
      
    //$('#androidApp li:first').remove(); //remove this first li ("Loading...") when done
	});


}


	// }
});	//	end jQuery document ready
