// Function to set a random or not so random 
// attraction banner at the top of each page
function setAttractionBanner(){
        var index = Math.floor(Math.random()*(bannerData.banners.length-1));
        jQuery('#attractionbanner').children().remove();
        jQuery('#attractionbanner').append('<a href="' + bannerData.banners[index].href + '" title="' + bannerData.banners[index].title + '">' +
    		                          '<img src="' +  bannerData.banners[index].image + '" alt="' + bannerData.banners[index].alt + '" /> </a>');
}

// create pop-up window by giving your anchor tag class="popup" - no other action needed
function createPopupOpener(width,height) {
  return function() {
    window.open(this.href, "", "top=40,left=40,width="+width+",height="+height+",resizable=yes,scrollbars=yes,toolbar=yes");
    return false;
  } 
}

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  linkRE = new RegExp(/popup(?:(\d+)x(\d+))?/);
  for (var i=0; i < links.length; i++) {   
    if (match = linkRE.exec(links[i].className)) {
      var width  = match[1] ? isNaN(match[1]) ? 650 : match[1] : 650
      var height = match[2] ? isNaN(match[2]) ? 625 : match[2] : 625
      var f = createPopupOpener(width,height)
      links[i].onclick=f
    } 
  } 
} 

// You open the popup by assigning class="popup" to the anchor tag 
// like this:  <a href="http://www.gullbuy.com" class="popup">Search 
// By Name</a></li>
//
// To specify a different size, specify widthxheight after the word popup, e.g.
// class="popup300x200" will open up a popup window with a width of 300 pixels
// and a height of 200 pixels.

// ---------------------------------------------------------------------
// to randomly change the SlideShow image on the page every 30 seconds
function pickRandom(range) 
{ 
	return pickRandom(1, range + 1);
}

function pickRandom(start, end){
	return num = Math.floor( Math.random() * (end + start));
}

function rollImage( image_total_number ){ 
		var delay = 30000;
		var $header_images = $("#header .carousel");
		var $image_position = pickRandom(0, $header_images.length);
		var $current_image = $header_images[$image_position];
		var $current_image_src = $current_image.src;
		var $new_image_path = '';		
		var val =  pickRandom(1, image_total_number) ;
		var new_src = $current_image_src.replace(/(.*)(\d+)\.(\w+)$/, "$1" + val + ".$3");
		$current_image.src = new_src;
		setTimeout( 'rollImage('+ image_total_number +')' , delay );
    
}

jQuery(document).ready (function() {
    //set the random attraction banner
    //setAttractionBanner();
			// call the popups script on line 2											
			doPopups();
			// call the image carousel script on line 33		
			//if($("#header .carousel").length > 0){			
			//	rollImage(2);											
			//}
})

//http://pure-essence.net/stuff/webTips/dodosTextCounter/index.html
//Dodo's Text Counter
jQuery.fn.dodosTextCounter=function(a,b){b=$.extend({counterDisplayElement:"span",counterDisplayClass:"dodosTextCounterDisplay",addLineBreak:true},b);$(this).each(function(i){updateCounter(this,a,b,i);$(this).keyup(function(){updateCounter(this,a,b,i);return this})});return this};function updateCounter(a,b,c,d){var e=0;var f=$(a).val();if(f){e=f.length}if(e>b){$(a).val(f.substring(0,b))}else{var g=b-e;var h=c.counterDisplayElement+"."+c.counterDisplayClass+":eq("+d+")";var i=$(h).length==0;if(i){var j=document.createElement(c.counterDisplayElement);if(c.counterDisplayElement=='input'){$(j).val(g.toString())}else{$(j).html(g.toString())}$(j).addClass(c.counterDisplayClass).insertAfter($(a));if(c.addLineBreak){$(a).after("<br />")}}else{if(c.counterDisplayElement=='input'){$(h).val(g.toString())}else{$(h).html(g.toString())}}}}


