$(function() {
	$('#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 #fff'
	});
	
	Cufon.replace('#reseller-header h2', {
		hover: true,
		textShadow: '1px 1px #fff'
	});

	Cufon.replace('#reseller-header p', {
		textShadow: '1px 1px #fff'	
	});
	
	Cufon.replace('#affiliates-header .left h2', {
		hover: true,
		textShadow: '0 2px #fff'
	});
	
	Cufon.replace('#affiliates-header .left p', {
		textShadow: '0 1px #6c246a'	
	});

	$('#phone-make, #phone-model, .select-list .list, .locked-to, .card-expiry-holder .select, #carrier1, #carrier2, #carrier3, #carrier4, #carrier5, #carrier6, #carrier7').sSelect({ddMaxHeight: '400px'});

	$('.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;
	});

	var logoWidth = $('#manufacturer-logo-desc').width();
	var logoHeight = $('#manufacturer-logo-desc').height();
	var logoPadding = $('#manufacturer-logo-desc').css('padding-left');
	var firstParagraphHeight = $('.sub-left p:first').height();
	var secondParagraphHeight = $('.sub-left p:first').next('p').height();
	var firstParagraphPadding = $('.sub-left p:first').css('padding-bottom');
	
	$('.sub-left p:first').next('p').css({'padding-left' : parseFloat(logoPadding) * 2 + logoWidth +  'px'});
	
	if(logoHeight >= 100) {
	var secondParagraphHeight = $('.sub-left p:first').next('p').height() + 10;
	$('#manufacturer-logo-desc').css({'margin-top' : firstParagraphHeight + 55 + 'px'});
	} else {
	$('#manufacturer-logo-desc').css({'margin-top' : firstParagraphHeight + secondParagraphHeight + parseFloat(firstParagraphPadding) - 10 + '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'});
	});

	$('.faq li').children('ul').hide();

	$('.faq li a').click(function() {
		var checkElement = $(this).next();

		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
	});

	$('.faq li').click(function () {
		$('.faq li').children('ul').slideUp('fast');

		$('.faq li > a').each(function() {
			if($(this).attr('rel') != '') {
				$(this).removeClass($(this).attr('rel') + 'selected');
			}
		});

		$(this).children('ul').slideDown('fast');
		$(this).children('a').addClass($(this).children('li a').attr('rel') + 'selected');

		return false;
	});

	$('#carrier-change').removeAttr('href').css('cursor', 'pointer');

	$('#carrier-country').change(function() {
		var country_val = $(this).val();

		$.ajax ({
			type: 'POST',
			url: 'ajax/carrier_list.php',
			cache: false,
			data: 'model_id=' + model_id + '&country=' + country_val,

			success: function(result) {
				$('#carrier').html(result).resetSS();
			}
		});
	});

	$('#submit').click(function() {			
		var carrier_id = $('#carrier').val();

		if($('#carrier-country').val() == 'default' || $('#carrier').val() == 'default') {
			$('#notification-container').alertMessage({
				alertMessage: 'You need to select a country and carrier before you can ... see the next screen'
			});
			return false;
		}
		else {
			$('#hidden-model_id').val(model_id);
			$('#hidden-carrier_id').val(carrier_id);

			if($('#hidden-carrier_id').val() == '' || $('#hidden-carrier_id').val() == null || $('#hidden-carrier_id').val() == 'Carrier') {
				$('#notification-container').alertMessage({
					alertMessage: 'Oops. Something went wrong, we\'re looking into it. Please go back and and try again.'
				});
				return false;
			}
			else
			{
				$('#carrier-container').fadeOut(800, function() {
					$('.unlocking-price').fadeIn(800);
					Cufon.replace('.phone-img #saving-percent');
					$('.phone-img .saving').fadeIn(800);
				});

				$.ajax ({
					url: 'http://www.unlocking.com/ajax/phone_details.php?model_id=' + model_id + '&carrier_id=' + carrier_id,
					cache: false,
					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('' + price);
							$('.carrier span').html(carrier);
							$('#ratings-total').html(ratings_total);
							$('#reviews-amount').html(reviews_total);
						});
					}
				});
			}
		}
	});

	$('#carrier-change').click(function() {
		$.ajax ({
			type: 'GET',
			cache: false,
			url: 'http://www.unlocking.com/ajax/countries.php?model_id=' + model_id,

			success: function(countries) {
				$('#carrier-country').html(countries).resetSS();
				return false;
			}
		});

		$('#carrier').html('<option value="default">Carrier</option>').resetSS();
		$('#hidden-model_id').val('');
		$('#hidden-carrier_id').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('http://assets.unlocking.com/imgs/sub-nav-hover-bg.png', 'http://assets.unlocking.com/imgs/affiliates-bg.png');

var hash = false;
urlHash();

function urlHash() {  
	if(window.location.hash != hash) {  
		hash = window.location.hash.substring(1);  
	}
}
