$(document).ready(function() {
	// Remove no-js tag if javascript is enabled
	$('html').removeClass('no-js');
	
	$('.dropdown').mouseenter(function(){
		$('.sublinks').stop(false, true).hide();
		$('.subdroplinks').stop(false, true).hide();
	
		var submenu = $(this).next();
 
		submenu.css({
			position:'absolute',
			top: ($(this).offset().top + $(this).height() + 12) + 'px',
			left: $(this).offset().left + 'px',
			zIndex: 999
		});
		
		submenu.stop().slideDown(300);
		
		submenu.mouseleave(function(){
			$(this).slideUp(300);
		});
		
		$(this).mouseleave(function(){
			submenu.delay(3000).slideUp(300);
		});
	});
	
	$('.subdropdown').mouseenter(function(){
		$('.subdroplinks').stop(false, true).hide();
	
		var subdropmenu = $(this).next();
 
		subdropmenu.css({
			position:'absolute',
			top: '0px',
			left: '150px',
			zIndex:1000
		});
		
		subdropmenu.stop().slideDown(300);
		
		subdropmenu.mouseleave(function(){
			$(this).slideUp(300);
		});
	});
	
	$('#industryNewsAddLink').click(function(){
		toggleIndustryLinksSignupForm();
		return false;
	})
	
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});

	
	// member signup functions	// Member payment click
	$('.finalstep').click(function(){
		$('#paymentformarea').html('<img src="http://www.gotrucker.com/images/loading.gif" border="0"/>');
		
		$.ajax({
			  url: '/ajax/generate_payment_form/' ,
			  data: $("#registration_form").serialize(),
			  dataType: 'json',
			  success: function(data) {
				if(data == null)
				    return;			
				    
				if(typeof(obj.errors) == 'object')
				{
					$('#paymentformarea').html(obj.errors);
				}
				else
				{
					$('#paymentformarea').html(obj.details);
					$('#payment_form').html(obj.form);
				}
			    
			  }
			});
			
	});
	
	$(".videoselection").click(
	  function () {
		$(".videoselection").removeClass('selected');
			
		$(this).addClass('selected');
		$.each($(".videoselection"), 
			function(key, val){
				if(!$("#" + val.id).hasClass('selected'))
					$(this).css("border", 'none').css("background", 'none');	   
			}
		);

		$('#'+ $(this).attr('id') + ' input').attr('checked', true);
		$(this).css("border", '1px solid black').css("backgroundColor", '#f6f6f6');
		$(this).css("background", '#d7d7d7 url(http://www.gotrucker.com/images/accept.gif) no-repeat');
			
	  }
	);
	
	
	$(".memberprice").click(
	  function () {
		$(".memberselection").removeClass('selected');
			
		$('input[id=input_' + $(this).attr('id') + ']').attr('checked', true);
		$(this).addClass('selected').css("background", '#d7d7d7 url(http://www.gotrucker.com/images/accept.gif) no-repeat');
		$.each($(".memberselection"), 
			function(key, val){
				if(!$("#" + val.id).hasClass('selected'))
					$(this).css("border", 'none').css("background", 'none');	   
			}
		);
			
	  }
	);
	
	
	$(".additionalfeature").click(
	  function () {		
			var $checkbox = $(this).find(':radio');
			if(!$(this).hasClass('selected'))
			{
				$('input[id=input_' + $(this).attr('id') + ']').attr('checked', true)
				$(this).addClass('selected').css("background", '#d7d7d7 url(http://www.gotrucker.com/images/accept.gif) no-repeat');
			}
			else
			{
				$('input[id=input_' + $(this).attr('id') + ']').attr('checked', false)
				$(this).removeClass('selected');
			}
	  }
	);
	
	$(".memberselection, .additionalfeature, .videoselection").hover(
	  function () {
			if(!$(this).hasClass('selected'))
				$(this).css("border", '1px solid black').css("backgroundColor", '#f6f6f6');
		
	  }, 
	  function () {
			if(!$(this).hasClass('selected'))
				$(this).css("border", 'none').css("background", 'none');
	  }
	);
	
	if ($( "#search_box input#state").length > 0) {
		$( "#search_box input#state").focus();
	}
});

var categoryhelper = {
	populatecategories: function (id) {	
		//$('select#category').html($("<option></option>\r\n").val('').html('Choose an option'));
		$.ajax({
			  url: '/ajax/getcategories' ,
			  dataType: 'json',
			  success: function(data) {
				if(data == null)
				    return;			
				    
			    $.each(data, function(key, item) {
				    if(id != null && id != '' && (id == item.id))
				    	$('select#category').append($("<option></option>\r\n").val(item.id).html(item.name).attr('selected', 'selected'));
				    else
					    $('select#category').append($("<option></option>\r\n").val(item.id).html(item.name));
			    });
			    
			  },
			  error: function(object, text, error) {
			  }
			});
	}
}



var dayOfWeek = new Array();
dayOfWeek[0] = "Sunday";
dayOfWeek[1] = "Monday";
dayOfWeek[2] = "Tuesday";
dayOfWeek[3] = "Wednesday";
dayOfWeek[4] = "Thursday";
dayOfWeek[5] = "Friday";
dayOfWeek[6] = "Saturday";

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function getMonday(d) {
  var day = d.getDay(),
      diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  return new Date(d.setDate(diff));
}

function toggleIndustryLinksSignupForm() {
	$('#industryNewsList').toggle();
	$('#industryNewsForm').toggle();
}

function showLoadingPopup(force){
	if(((loading_attempt < loading_attempt_limit)
		&& ((url.search(/dashboard/g) != -1)
		|| (url.search(/ads/g) != -1)))
		|| (force == true))
	{
		if((loading_scripts == false) || (force == true))
		{
			$('#loading_notice').dialog({ 
				modal: true,
				height: 130, 
				width: 100,
				autoOpen: true,
				buttons: {
					"Close": function(){
						$(this).dialog("close");
					}
				},
				position: ['top', 'center']
			});
			$(".ui-dialog-titlebar").hide();
		   	loading_scripts = true;

		}

		loading_attempt++;
	}

}

function hideLoadingPopup(){
	$('#loading_notice').dialog('close');

}

function formatNumber(num)
{    
    var n = num.toString();
    var nums = n.split('.');
    var newNum = "";
    if (nums.length > 1)
    {
        var dec = nums[1].substring(0,2);
        newNum = nums[0] + "." + dec + '%';
    }
    else
    {
    newNum = num;
    }
    return newNum
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}






// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function convertObjectToArray(object){
	var newArray = []
	for (var key in object) {
	    newArray.push(object[key]);
	}
	
	return newArray;
}

function stateSearchBoxValue(){
	return $('#search_box input#state').val();
}
function citySearchBoxValue(){
	return $('#search_box input#city').val();
}
