//<!--

$(document).ready( function()
{
	// PNG image fix for IE6
	$(document).pngFix();
	
	// Slide toggles
	$('.slide_toggle').click(function(){
		$( $(this).attr('rel') ).slideToggle('slow');
		return false;
	});
	
	// Toggle feedback boxes
	$('.feedback_box').click(function(event)
	{
		event.preventDefault();
		event.stopImmediatePropagation();
		$(this).toggle();
		return false;
	});
	
	// Submit the closest form on element click
	$('.click_submit_form').click(function(){
		$(this).closest('form').submit();
		return false;
	});
	
	// Tooltips
	$('.qtip_tooltip').qtip({
		show: { 
			delay: 0, 
			solo: true, 
			when: { event: 'mouseover' } 
		},
		hide: 'mouseout',
		style: { 
			name: 'cream', 
			tip: true 
		},
		position: {
			corner: {
				target: 'topRight',
				tooltip: 'bottomLeft'
			}
		}
	}).click(function(event){return false;});
	
	// Form input default value toggling
	$('.search-box input[name=q]').logsdon_toggle_val({
		'default_val': 'Name of Provider', 
		'blur_class': 'default'
	});
	$('input.search_text[name=city]').logsdon_toggle_val({
		'default_val': 'City, ST', 
		'blur_class': 'default'
	});
	
	// Print page and record analytics hit
	$('.print_click').click(function(event)
	{
		event.preventDefault();
		event.stopImmediatePropagation();
		
		record_page_hit( $(this).attr('rel') );
		window.print();
		
		return false;
	});
	
	// Print page and record analytics hit
	$('.analytics_click').click(function(event)
	{
		record_page_hit( $(this).attr('rel') );
		return true;
	});
	
	$('a.inline').fancybox({'hideOnContentClick': true, 'overlayOpacity': 0.3, 'padding': 0});
	
	// Prep dropdown submenu
	$('li.headlink').hover(
		function() { $('ul', this).css('display', 'block'); },
		function() { $('ul', this).css('display', 'none'); }
	);
	/*
	$(document).ready(function(){
	});*/
	/* or without jQuery:
	window.onload = function()
	{
		var lis = document.getElementById('cssdropdown').getElementsByTagName('li');
		for(i = 0; i < lis.length; i++)
		{
			var li = lis[i];
			if (li.className == 'headlink')
			{
				li.onmouseover = function() { this.getElementsByTagName('ul').item(0).style.display = 'block'; }
				li.onmouseout = function() { this.getElementsByTagName('ul').item(0).style.display = 'none'; }
			}
		}
	}
	*/
	
	// Setup modal window
	if ( $('.iframe_opt_in').length > 0 )
	{
		$('.iframe_opt_in').fancybox({ 
			'frameWidth': 408
			, 'frameHeight': 250
			, 'overlayOpacity': 0.3
			, 'padding': 0
			, 'hideOnContentClick': false
			, 'centerOnScroll': false
			//, 'callbackOnClose': function () { $('#fancy_frame').remove(); }
		});
	}
	
	if ( $('.iframe_sign_in').length > 0 )
	{
		$('.iframe_sign_in').fancybox({ 
			'frameWidth': 408
			, 'frameHeight': 270
			, 'overlayOpacity': 0.3
			, 'padding': 0
			, 'hideOnContentClick': false
			, 'centerOnScroll': false
			//, 'callbackOnClose': function () { $('#fancy_frame').remove(); }
		});
	}
	
	if ( $('.iframe_sign_up').length > 0 )
	{
		$('.iframe_sign_up').fancybox({ 
			'frameWidth': 408
			, 'frameHeight': 270
			, 'overlayOpacity': 0.3
			, 'padding': 0
			, 'hideOnContentClick': false
			, 'centerOnScroll': false
			//, 'callbackOnClose': function () { $('#fancy_frame').remove(); }
		});
	}
	
	if ( $('.iframe_forward').length > 0 )
	{
		$('.iframe_forward').fancybox({ 
			'frameWidth': 408
			, 'frameHeight': 270
			, 'overlayOpacity': 0.3
			, 'padding': 0
			, 'hideOnContentClick': false
			, 'centerOnScroll': false
			//, 'callbackOnClose': function () { $('#fancy_frame').remove(); }
		});
	}
	
	$('a.clicktrack2').click(function(event)
	{
		$.post( base_url +'tracking/click', {'id': this.id, 'ref': encodeURIComponent(document.location.pathname)} );
		return true;
	});
	
	//http://stackoverflow.com/questions/1889190/jquery-click-tracking-with-php
	$.fn.track = function ()
	{
		//var localserver = "www.prodevtips.com"; 
		//$("a").not(".kLink").not("a[href^='http://"+localserver+"']").click(function(){ 
		//$(this).attr("target", "blank"); 
		
		var source, url, name, ref, $this;
		$this = $(this);
		
		if ( window.location.search.substring(1) != '' )
		{
			source = window.location.pathname +'?'+ window.location.search.substring(1);
		}
		else
		{
			source = window.location.pathname;
		}
		
		url = encodeURIComponent($this.attr('href'));
		name = $this.attr('name');
		ref = encodeURIComponent(source);
		
		$this.live('click', function (event)
		{
			event.preventDefault();
			
			$.post( 
				base_url +'tracking/click',
				{
					url: url,
					name: name,
					ref: ref
				}, 
				function (data)
				{
					var target = $this.attr('target');
					var href = $this.attr('href');
					
					if ( target == '' || target == '_self' )
					{
						window.location = href;
					}
					else if ( target == '_blank' )
					{
						window.open = href;
					}
					else if ( target == '_top' )
					{
						window.parent.location = href;
					}
				}
			);
			
			return false;
		});
	};
	
	$('a.clicktrack').track();

});

/** 
* 
*/
function record_page_hit( page )
{
	if ( page.length > 0 )
	{
		// Change iframe src to make a faux page hit with some relevance
		$('#iframe_analytics').attr('src', base_url +'home/analytics/'+ page);
	}
}

/** 
* 
*/
function updateSiteEmailFields()
{
	// Populate e-mail fields
	var email = $.cookies.get('email');
	if (email != 'undefined' && email != 'null' && email != null && email != '')
	{
		$('input[name=paintrelief_com_3mayle]').each(function(index){
			$(this).val( email );
		});
	}
}

/**
*
*/
function limitText( limitField, limitCount, limitNum, doNotLimit )
{
	if ( limitField.value.length > limitNum && doNotLimit == true )
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		$('#'+ limitCount).html(limitNum - limitField.value.length);
	}
}

/** 
* 
*/
function toggle_clickHandler( event, element_id )
{
	if (event) event.preventDefault();
	$(element_id).slideToggle('fast');
	return false;
}

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
//http://www.openjs.com/scripts/others/dump_function_php_print_r.php
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

//-->