//*****************************************************************
// PNG Fixes
//*****************************************************************
$( document ).ready ( function() {
	//$("#movie_skin").pngfix();
	//$("#content_container").pngfix({sizingMethod: "scale"});
	//$("#content").pngfix({sizingMethod: "scale"});
} );

//*****************************************************************
// Swap BG image
//*****************************************************************
$( document ).ready ( function() {	
    // Swap out the background image if less than 1024px wide
    if (GetWidth() <= 1024) {
        $("#mainer").css('background-image','url(../images/layout/page_bg_less_1024.jpg)');
    }
} );


//*****************************************************************
// Bookmark
//*****************************************************************
$(document).ready(function(){
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){
			$("a.jqbookmark").attr("rel","sidebar");
		} 
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;
		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else { 
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}
	
	});
});

//*****************************************************************
// Don't show bookmark link of IE 6
//*****************************************************************
$( document ).ready ( function() {
    jQuery.each(jQuery.browser, function(i, val) {
        if($.browser.msie && $.browser.version == "6.0"){
            $(".jqbookmark").remove();
        }
    });
});

//*****************************************************************
// Callback Form
//*****************************************************************
$(document).ready ( function()
{
	var cb_form = $('form[name="callback_form"]');
    var is_form = ($(cb_form).length > 0) ? (true) : (false);
	
	if(is_form)
	{
		var country = $(cb_form).find('select[name="country"]').val();
		
		if(country != '') { display_formatting(country); }
		
		$(cb_form).find('select[name="country"]').change( function()
		{
			country = $(this).val();
			display_formatting(country);
		});
		
		$(cb_form).find('input[name="phone"]').blur( function()
		{
			var phone = $(this).val();
			phone = phone.replace(/[^0-9]/g, '');
			
			switch(country)
			{
				case 'US':
				case 'CA':
					var regex = /^[1]{1}[2-9]{1}[0-8]{1}[0-9]{1}[2-9]{1}[0-9]{6}$/;
					var regex_country_code = /^[1]{1}/;
					break;
				
				case 'GB':
					var regex = (phone.length == 12) ? (/^[4]{2}[0-9]{2}[0-9]{4}[0-9]{4}$/) : (/^[4]{2}[0-9]{5}[0-9]{4}$/);
					var regex_country_code = /^[4]{2}/;
					break;
				
				case 'AU':
					var regex = /^[6,1]{2}[1-9]{1}[0-9]{4}[0-9]{4}$/;
					var regex_country_code = /^[6,1]{2}/;
					break;
				
				default:
			}
			
			var is_valid = (phone.match(regex) != null) ? (true) : (false);
            
			if(is_valid === false || phone.match(regex_country_code) == null)
			{
				$(this).val('Phone Number Is Invalid');
				
				$(this)
					.css({color: '#FF0000'})
					.animate({opacity: '0.1'}, 500)
					.animate({opacity: '1'}, 500)
					.animate({opacity: '0.1'}, 500)
					.animate({opacity: '1'}, 500)
					
				window.setTimeout( function() {
					$(cb_form).find('input[name="phone"]').val(phone);
				}, 2500);
			}
		});
		
		$(cb_form).find('input[name="phone"]').focus( function()
		{
			$(this).css('color', '#000000');
		});
	}
});

//*****************************************************************
// Display formatting of phone numbers for callback form
//*****************************************************************
function display_formatting(c)
{
	var phone_format = '';
	
	switch(c)
	{
		case 'US':
		case 'CA':
			phone_format = 'eg: <i>1 (xxx) xxx xxxx</i>';
			break;
		case 'GB':
			phone_format = 'eg: <i>44 xx xxxx xxxx</i> or <i>44 xxxxx xxxx</i>';
			break;
		case 'AU':
			phone_format = 'eg: <i>61 x xxxx xxxx</i>';
			break;
		default:
			
	}
	$('#phone_format').html(phone_format);
}

//*****************************************************************
// URL encode
//*****************************************************************
function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
                                     
    var ret = str;
    
    ret = ret.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
 
    return ret;
}

//*****************************************************************
// Show Chatwise chat box
//*****************************************************************
$( document ).ready ( function() {
    $("#chatlink").click(function () {
        $("#chatbox_container").toggle(300);
        return false;
    });
} );


//*****************************************************************
// Get browser width
//*****************************************************************
function GetWidth()
  {
          var x = 0;
          if (self.innerHeight)
          {
                  x = self.innerWidth;
          }
          else if (document.documentElement && document.documentElement.clientHeight)
          {
                  x = document.documentElement.clientWidth;
          }
          else if (document.body)
          {
                  x = document.body.clientWidth;
          }
          return x;
  }



