var $j = jQuery.noConflict();//to avoid conflicts with other libraries 

$j(document).ready(function() {//on JQuery doc ready
	 setTimeout("Landing.init();", 500);//even tho this is onload, browsers need a sec to DL images
})
 
 
var Landing = new function() {

	 this.init = function() {
		 
		 $j('#page_wrapper1.alt_width').each(function(){//custom header used, so calculate width on the fly
			 var newWidth =
			 parseInt( $j('img:first','#header').width() )
			 + getCssAtrr('#page_wrapper2', 'border-left-width')
			 + getCssAtrr('#page_wrapper2', 'border-right-width')
			 + getCssAtrr('#page_wrapper2', 'padding-left')
			 + getCssAtrr('#page_wrapper2', 'padding-right');

			 if ( !isNaN(newWidth) ) {
				 $j(this).css('width', newWidth + "px" );
			 }
		 })
		 
		 $j('.q_a_wrapper > label', '.actual_content').each(function(){
			 $j(this)
			 	.after('<br />')
			 	.html( $j(this).html().replace("*", "") );
			 
			 var labelText = $j(this).html();
			 
			 var inputID = $j(this).attr('for');
			 
			 if ( inputID.indexOf('phone')==-1 ) {
				 if ( $j('input[id='+inputID+']').attr('type')=='text' ) {
					 $j('input[id='+inputID+']')
					 	.attr('labelText', labelText)
					 	.val(labelText)
					 	.focus(function(){
					 		if ( $j(this).val()==labelText ) {
					 			$j(this).val('');
					 		}
					 	})
					 	.blur(function(){
					 		if ( $j(this).val()=='' ) {
					 			$j(this).val(labelText);
					 		}
					 	});
					 
					 $j(this).hide();
				 }
			 }
			 
			 if ( inputID.indexOf('country')!=-1 ) {
				 $j(this).hide();
			 }
		 })
		 
		 // START replace tokens
		 $j('#crsDaysLeft').each(function(){
			 Landing.getCrsDaysLeft();
		 })
		
		 
		 if ( $j('#main').size() > 0 ) {
			 var str = $j('#main').html();
			 
			 var patt1=/___(.*?)___/gi;
			 var matches = str.match(patt1);
						 
			 if ( matches ) {
				 for( j=0; j < matches.length; j++ ) {
					 var thisMatch = matches[ j ];
		
					 var divName = thisMatch.substring( thisMatch.indexOf('___')+3, thisMatch.lastIndexOf('___') );
					 divName = divName.toLowerCase();
					 
					 var divVal = "";
					 
					 divVal = $j('.'+divName, '#hiddenAttrs').html();
					 
					 var newHTML = $j('#main').html().replace(new RegExp(thisMatch, 'g'), divVal);
					 newHTML = newHTML.replace(/\&lt;/g,"<").replace(/\&gt;/g,">");
					 $j('#main').html( unescape(newHTML) );
				 }
			 }
		 }
		// END replace tokens		
	 }//init
	 
	 //f = callback function
	 this.formatCrtName = function(f) {		 
		//use a clone of the original name field to figure out if the name has to be resized to fit horizontally
		 var $nameTester = $j('.name_placeholder:first').clone()
		 	.attr('id', 'nameTester')
		 	.css({width: 'auto', position: 'absolute', visibility: 'hidden', 'white-space': 'nowrap'});
		 
		 $j('body').append($nameTester);

		 if ( parseInt($j('#nameTester').width()) > parseInt($j('.name_placeholder').width()) ) {
			 while ( parseInt($j('#nameTester').width()) > parseInt($j('.name_placeholder').width()) ) {
				 var currentFontSize = parseInt( $j('#nameTester').css('font-size') );
				 $j('#nameTester').css('font-size', (currentFontSize - 1) + 'px' )
			 }
			 
			 var newFontSize = parseInt( $j('#nameTester').css('font-size') );
			 $j('.name_placeholder').css('font-size', newFontSize + 'px');
		 }
		 
		 $j('#nameTester').remove();
		 
		 if (typeof f == "function") f();
	 }
	 
	 this.getCrsDaysLeft = function() {
		 var post_url = "/mysite/ajaxHandler.php";
		 var post_data = {};
		 post_data.action = 'getCrsDaysLeft';
		 post_data.crs = $j('.name:first', '#hiddenAttrs').html();
		 
		$j.post(post_url, post_data, function(resp){
			$j('#crsDaysLeft').html(resp);
		})
	 }
	 
 }

 function getCssAtrr(elem, attr){
	 var val = 0;
	 if (   !isNaN( parseInt($j(elem).css(attr)) )   ) {
		 val = parseInt( $j(elem).css(attr) )
	 }
	 return val;
 }

