$(function() {
	// initialize stylish selects
	$('#phone-model, .select-list .list, .locked-to, .card-expiry-holder .select, #kb-choose-phone').sSelect({ddMaxHeight: '250px'});

	$('#header-search-input, #notify-email-address, .affiliates-login-hold .input, #knowledgebase-sub-right .input').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});

	$('#main-unlock').hover(function() {
		$('#sub-how, #sub-support').hide();
		$('#sub-unlock').show();
		$('#main-unlock, #main-how, #main-support').removeClass('current');

		$(this).mouseout(function() {
			$(this).addClass('current');
		});
	});

	$('#main-how').hover(function() {
		$('#sub-unlock, #sub-support').hide();
		$('#sub-how').show();
		$('#main-unlock, #main-how, #main-support').removeClass('current');

		$(this).mouseout(function() {
			$(this).addClass('current');
		});
		
		$('#main-unlock').addClass('nav-sep');
	});

	$('#main-support').hover(function() {
		$('#sub-unlock, #sub-how').hide();
		$('#sub-support').show();
		$('#main-unlock, #main-how, #main-support').removeClass('current');

		$(this).mouseout(function() {
			$(this).addClass('current');
		});

		$('#main-how').removeClass('nav-sep');
	});

	Cufon.replace('.header-content .description', {
		hover: true,
		textShadow: '0 1px #14394f'
	});


  Cufon.replace('h1, h2, h4, .process-number', {fontFamily: 'Brandon'});
  Cufon.replace('h3', {fontFamily: 'Brandon Reg'});
  
  // Cufon.replace('#reseller-header h2', {
  //  hover: true,
  //  textShadow: '1px 1px #111d1f'
  // });
  // 
  // Cufon.replace('#reseller-header p', {
  //  textShadow: '1px 1px #4e676e' 
  // });
  // 
  // Cufon.replace('#affiliates-header .left h2', {
  //  hover: true,
  //  textShadow: '0 2px #2e1a2d'
  // });
  // 
  // Cufon.replace('#affiliates-header .left p', {
  //  textShadow: '0 1px #6c246a' 
  // });


	// $('.get-started #phone-make').change(function() {
	// 	var make_id = $(this).val();
	// 
	// 	$.ajax ({
	// 		type: 'POST',
	// 		url: 'ajax/model_list.php',
	// 		data: 'makeid=' + make_id,
	// 
	// 		success: function(phone_model) {
	// 			$('#phone-model').html(phone_model).resetSS();
	// 			return false;
	// 		}
	// 	});
	// });

	// $('input:checkbox').each(function() {
	// 	(this.checked) ? $('#fake' + this.id).addClass('fakechecked') : $('#fake' + this.id).removeClass('fakechecked');
	// });
	// 
	// $('.fakecheck').click(function() {
	// 	($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
	// 	$(this.hash).trigger('click');
	// 	return false;
	// });
	// 	
	// $('#remember-me').click(function() {
	// 	($('#fake').hasClass('fakechecked')) ? $('#fake').removeClass('fakechecked') : $('#fake').addClass('fakechecked');
	// 	$('#fake-checkbox').trigger('click');
	// 	return false;
	// });
	
	/*
	 * phone-choose logo positioning
	 */
	var logoWidth = $('#manufacturer-logo-desc').width();
	var logoPadding = $('#manufacturer-logo-desc').css('padding-left');
	var firstParagraphHeight = $('.sub-left p:first').height();
	var secondParagraphHeight = $('.sub-left p:first').next('p').height() / 2;
	var firstParagraphPadding = $('.sub-left p:first').css('padding-bottom');

	$('#manufacturer-logo-desc').css({'margin-top' : firstParagraphHeight + secondParagraphHeight + parseFloat(firstParagraphPadding) / 2 + 'px'});
	$('.sub-left p:first').next('p').css({'padding-left' : parseFloat(logoPadding) * 2 + logoWidth +  'px'});
	
	// var get_desc_img_height = $('.browse-desc img').height();
	// var get_desc_height = $('.browse-desc p').height();
	// var center_sum = get_desc_height - get_desc_img_height;
	// var center = center_sum / 2;
	// $('.browse-desc img').css({'margin-top' : center + 'px'})
	
	// $('#popular-makes-list li img').each(function() {
	// 	var imgID = $(this).attr('id');
	// 	var imgHeight = $(this).height();
	// 	var minusHeight = 161 - imgHeight;
	// 	var marginTop = minusHeight / 2;
	// 
	// 	$('#' + imgID).css({'margin-top' : marginTop - 36 + 'px'});
	// });

	// 
	// $('#submit').click(function() {			
	// 	var carrier_id = $('#carrier').val();
	// 
	// 	if($('#carrier-country').val() == 'default') {
	// 		$('#notification-container').alertMessage({
	// 			alertMessage: 'You need to select a country and carrier before you can ... see the next screen'
	// 		});
	// 		return false;
	// 	}
	// 	else {
	// 		$('#carrier-container').fadeOut(800, function() {
	// 			$('.unlocking-price').fadeIn(800);
	// 			Cufon.replace('.phone-img #saving-percent');
	// 			$('.choose-carrier .phone-img .saving').fadeIn(800);
	// 		});
	// 
	// 		$('#hidden-modelid').val(model_id);
	// 		$('#hidden-carrierid').val(carrier_id);
	// 
	// 		$.ajax ({
	// 			url: 'phone-carrier.php', // remove this line when site is live
	// 			// url: 'http://speed.unlocking.com/ajax/phone_details.php?modelid=' + model_id + '&carrierid=' + carrier_id, // use this when site is online
	// 			dataType: 'xml',
	// 
	// 			success: function(xml) {				
	// 				$(xml).find('phone_details').each(function() {
	// 					var id = $(this).attr('id');
	// 					var savings = $(this).find('savings').text();
	// 					var list = $(this).find('list_price').text();
	// 					var price = $(this).find('price').text();
	// 					var carrier = $(this).find('carrier').text();
	// 					var time = $(this).find('time').text();
	// 					var ratings_total = $(this).find('ratings_total').text();
	// 					var rating = $(this).find('rating').text();
	// 					var reviews_total = $(this).find('reviews_total').text();
	// 
	// 					$('.list-price span').html(list);
	// 					$('.delivery-time span').fadeOut(500, function() {
	// 						$('.delivery-time span').html('Delivery time: ' + time).fadeIn(500);
	// 					});
	// 					$('#saving-percent').html('Save ' + savings);
	// 					$('.our-price span').html('&pound;' + price);
	// 					$('.carrier span').html(carrier);
	// 					$('#ratings-total').html(ratings_total);
	// 					$('#reviews-amount').html(reviews_total);
	// 				});
	// 			}
	// 		});
	// 
	// 		$.ajax ({
	// 			type: 'POST',
	// 			url: 'ajax/phone_detail.php',
	// 			data: 'carrierid=' + carrier_id + '&modelid=' + model_id
	// 		});
	// 	}				
	// });
	// 
	// $('#carrier-change').click(function() {
	// 	$('#carrier-country').val('default');
	// 	$('#carrier').html('<option>Carrier</option>');
	// 
	// 	$('#hidden-modelid').val('');
	// 	$('#hidden-carrierid').val('');
	// 
	// 	Cufon.replace('.phone-img #saving-percent');
	// 	
	// 	$('.unlocking-price').fadeOut(800, function() {
	// 		$('#carrier-container').fadeIn(800);
	// 	});
	// 
	// 	$('.delivery-time span').fadeOut(700, function() {
	// 		$(this).html('Delivery time can range 12-48 hours, based on your carrier. Please choose above...').fadeIn(700);
	// 	});
	// 	
	// 	$('#saving-percent').fadeOut(800).html('');
	// });
});

(function() {
	$.fn.extend ({
		alertMessage: function(options) {

			var defaults = {
				alertMessage: null,
				alertHeight: '66px',
				alertSpeed: 250,
				alertDuration: 5000
			}

			var options = $.extend(defaults, options);

			return this.each(function() {
				var o = options;

				if(o.alertMessage != null) {
					$(this).html('<span>' + o.alertMessage +'</span>')
					.animate({height : o.alertHeight}, o.alertSpeed)
					.delay(o.alertDuration)
					.animate({height : 0}, o.alertSpeed);
				}
			});
		}
	});
})(jQuery);

(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for(var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

jQuery.preLoadImages('/images/default/sub-nav-hover-bg.png', '/images/default/affiliates-bg.png');

