$( document ).ready( function(){
	setTotal();
	if ( $.browser.msie && $.browser.version.substr(0,1) == 6 ) {
	DD_belatedPNG.fix('.fader .box img');
	}	
	if ( $.browser.msie && $.browser.version.substr(0,1) == 6 ) {
	DD_belatedPNG.fix('.rightaligned img');
	}	
	
	if($.browser.msie ){
		$('.heading .left-area p').css('letter-spacing', '-0.04em')
	}
	if($.browser.safari ){
		$('.heading .left-area p').css('letter-spacing', '-0.05em')
	}
	if($.browser.opera ){
		$('.heading .left-area p').css('letter-spacing', '-0.03em')
	}
	
	$(function() {
		$(".pages .limitation").slider({
			value:0,
			min: 0,
			max: 1500,
			step: 150,
			slide: function(event, ui) {
				$(".pages .amount").val('$' + ui.value);
				$(".pages .amount-qty").val(ui.value/150);
				$(".pages .ui-slider-handle").html(ui.value/150);
				setTotal();
			}
		});
		$(".pages .amount").val('$' + $(".pages .limitation").slider("value"));
		$(".pages .ui-slider-handle").html($(".pages .limitation").slider("value")/150);
	});
	
	$(function() {
		$(".logos .limitation").slider({
			value:0,
			min: 0,
			max: 500,
			step: 50,
			slide: function(event, ui) {
				$(".logos .amount").val('$' + ui.value);
				$(".logos .amount-qty").val(ui.value/50);
				$(".logos .ui-slider-handle").html(ui.value/50);
				setTotal();
			}
		});
		$(".logos .amount").val('$' + $(".logos .limitation").slider("value"));
		$(".logos .ui-slider-handle").html($(".logos .limitation").slider("value")/75);
	});
			
	
	$(function() {
		$(".ideas .limitation").slider({
			value:0,
			min: 0,
			max: 500,
			step: 50,
			slide: function(event, ui) {
				$('input[name=nlogos]').parents('.l-fld').find('a.del').addClass('hidden');
				$(".ideas .amount").val('$' + ui.value);				
				$(".ideas .ui-slider-handle").html(ui.value/50);
				$(".ideas .nlogos").val(ui.value/50);
				setTotal();
			}
		});
		$(".ideas .amount").val('$' + $(".ideas .limitation").slider("value"));
		$(".ideas .ui-slider-handle").html($(".ideas .limitation").slider("value")/50);
		$(".ideas .nlogos").val($(".ideas .limitation").slider("value")/50);
	});
	$(function() {
		$(".1616 .limitation").slider({
			value:0,
			min: 0,
			max: 500,
			step: 50,
			slide: function(event, ui) {
				$(".1616 .amount").val('$' + ui.value);				
				$(".1616 .amount_qty").val(ui.value/50);
				$(".1616 .ui-slider-handle").html(ui.value/50);
				setTotal();
			}
		});
		$(".1616 .amount").val('$' + $(".1616 .limitation").slider("value"));
		$(".1616 .ui-slider-handle").html($(".1616 .limitation").slider("value")/50);
	});
	$(function() {
		$(".2424 .limitation").slider({
			value:0,
			min: 0,
			max: 500,
			step: 50,
			slide: function(event, ui) {
				$(".2424 .amount").val('$' + ui.value);				
				$(".2424 .amount_qty").val(ui.value/50);
				$(".2424 .ui-slider-handle").html(ui.value/50);
				setTotal();
			}
		});
		$(".2424 .amount").val('$' + $(".2424 .limitation").slider("value"));
		$(".2424 .ui-slider-handle").html($(".2424 .limitation").slider("value")/50);
	});
	
	$(function() {
		$(".3232 .limitation").slider({
			value:0,
			min: 0,
			max: 1000,
			step: 100,
			slide: function(event, ui) {
				$(".3232 .amount").val('$' + ui.value);				
				$(".3232 .amount_qty").val(ui.value/100);
				$(".3232 .ui-slider-handle").html(ui.value/100);
				setTotal();
			}
		});
		$(".3232 .amount").val('$' + $(".3232 .limitation").slider("value"));
		$(".3232 .ui-slider-handle").html($(".3232 .limitation").slider("value")/100);
	});
	
	$(function() {
		$(".4848 .limitation").slider({
			value:0,
			min: 0,
			max: 1250,
			step: 125,
			slide: function(event, ui) {
				$(".4848 .amount").val('$' + ui.value);				
				$(".4848 .amount_qty").val(ui.value/125);
				$(".4848 .ui-slider-handle").html(ui.value/125);
				setTotal();
			}
		});
		$(".4848 .amount").val('$' + $(".4848 .limitation").slider("value"));
		$(".4848 .ui-slider-handle").html($(".4848 .limitation").slider("value")/125);
	});
	
	$(function() {
		$(".6464 .limitation").slider({
			value:0,
			min: 0,
			max: 1500,
			step: 150,
			slide: function(event, ui) {
				$(".6464 .amount").val('$' + ui.value);				
				$(".6464 .amount_qty").val(ui.value/150);
				$(".6464 .ui-slider-handle").html(ui.value/150);
				setTotal();
			}
		});
		$(".6464 .amount").val('$' + $(".6464 .limitation").slider("value"));
		$(".6464 .ui-slider-handle").html($(".6464 .limitation").slider("value")/150);
	});
	
	$(function() {
		$(".128 .limitation").slider({
			value:0,
			min: 0,
			max: 1750,
			step: 175,
			slide: function(event, ui) {
				$(".128 .amount").val('$' + ui.value);				
				$(".128 .amount_qty").val(ui.value/175);
				$(".128 .ui-slider-handle").html(ui.value/175);
				setTotal();
			}
		});
		$(".128 .amount").val('$' + $(".128 .limitation").slider("value"));
		$(".128 .ui-slider-handle").html($(".128 .limitation").slider("value")/175);
	});
	
	$(function() {
		$(".256 .limitation").slider({
			value:0,
			min: 0,
			max: 2750,
			step: 275,
			slide: function(event, ui) {
				$(".256 .amount").val('$' + ui.value);				
				$(".256 .amount_qty").val(ui.value/275);
				$(".256 .ui-slider-handle").html(ui.value/275);
				setTotal();
			}
		});
		$(".256 .amount").val('$' + $(".256 .limitation").slider("value"));
		$(".256 .ui-slider-handle").html($(".256 .limitation").slider("value")/275);
	});
	
	$(function() {
		$(".512 .limitation").slider({
			value:0,
			min: 0,
			max: 3750,
			step: 375,
			slide: function(event, ui) {
				$(".512 .amount").val('$' + ui.value);				
				$(".512 .amount_qty").val(ui.value/375);
				$(".512 .ui-slider-handle").html(ui.value/375);
				setTotal();
			}
		});
		$(".512 .amount").val('$' + $(".512 .limitation").slider("value"));
		$(".512 .ui-slider-handle").html($(".512 .limitation").slider("value")/375);
	});
	
	$(function() {
		$(".psd-main .limitation").slider({
			value:0,
			min: 0,
			max: 8500,
			step: 850,
			slide: function(event, ui) {
				$('input[name=mainpages_qty]').parents('.l-fld').find('a.del').addClass('hidden');
				$(".psd-main .amount").val('$' + ui.value);
				$(".psd-main .amount-qty").val(ui.value/850);
				$(".psd-main .ui-slider-handle").html(ui.value/850);
				setTotal();
			}
		});
		$(".psd-main .amount").val('$' + $(".psd-main .limitation").slider("value"));
		$(".psd-main .ui-slider-handle").html($(".psd-main .limitation").slider("value")/850);
	});
	
	$(function() {
		$(".psd-sub .limitation").slider({
			value:0,
			min: 0,
			max: 1500,
			step: 150,
			slide: function(event, ui) {
				$('input[name=mainpages_qty]').parents('.l-fld').find('a.del').addClass('hidden');
				$(".psd-sub .amount").val('$' + ui.value);
				$(".psd-sub .amount-qty").val(ui.value/150);
				$(".psd-sub .ui-slider-handle").html(ui.value/150);
				setTotal();
			}
		});
		$(".psd-sub .amount").val('$' + $(".psd-sub .limitation").slider("value"));
		$(".psd-sub .ui-slider-handle").html($(".psd-sub .limitation").slider("value")/150);
	});
	
	$(function() {
		$(".style .limitation2").slider({
			value:2,
			min: 1,
			max: 3,
			step: 1,
			slide: function(event, ui) {
				$('.style .style-input').val(ui.value);
				$('.style .amount').val(ui.value);
				$(ui.handle).parents('.style').find('.style-amnt').val(ui.value);
			}
		});
	});
	
	$(function() {
		$(".app-sec .limitation").slider({
			value:0,
			min: 0,
			max: 2500,
			step: 250,
			slide: function(event, ui) {
				$(".app-sec .amount").val('$' + ui.value);
				$(".app-sec .amount-qty").val(ui.value/250);
				$(".app-sec .ui-slider-handle").html(ui.value/250);
				setTotal();
			}
		});
		$(".app-sec .amount").val('$' + $(".app-sec .limitation").slider("value"));
		$(".app-sec .ui-slider-handle").html($(".app-sec .limitation").slider("value")/250);
	});
	
	$(function() {
		$(".ipad .limitation").slider({
			value:0,
			min: 0,
			max: 5500,
			step: 550,
			slide: function(event, ui) {
				$(".ipad .amount").val('$' + ui.value);
				$(".ipad .amount-qty").val(ui.value/550);
				$(".ipad .ui-slider-handle").html(ui.value/550);
				setTotal();
			}
		});
		$(".ipad .amount").val('$' + $(".ipad .limitation").slider("value"));
		$(".ipad .ui-slider-handle").html($(".ipad .limitation").slider("value")/550);
	});
	
	function setTotal() {
		var total = 0;
		
		
		$('.amount').each(function() {
			var str = $(this).val();
			str = str.replace('$', '');
			str = parseInt(str);
			total+= str;
		});
		var wp = $('#wordpress').val();
		if (wp == 'Yes-250') {
			total += 250;
		}
		
		$('#total').html(total);
		$('#amount_total').val(total);
		
		calculate_delivery();
	}
	$('.dropdown').change(function(){
		setTotal();
	});

	start_slides( '.fader .box img', 4500 );
	start_slides( '.testimonial p', 4500 );
	
	$('#file').customFileInput();	
	
	$('.dropdown').sSelect();
	
	$(function(){
    	$('.field').
    	    focus(function() {
    	        if(this.title==this.value) {
    	            this.value = '';
    	        $(this).parent().css('background-position', '0 bottom');
    	        }
    	    }).
    	    blur(function(){
    	        if(this.value=='') {
    	            this.value = this.title;
    	            $(this).parent().css('background-position', '0 0')
    	        }
        });
    })
	
	$('a.del').click(function(){
		$(this).parent().find('a').hide();
		var text = '';
		 $(this).parent().find('input').val(text);
		 $(this).parent().find('input').focus();
		return false
	});
	$('a.accept').click(function(){	
		$(this).parent().find('a').hide();
		$(this).parent().css('background-position', '0 0');
		$(this).parent().parent().next().find('.field').focus();
		
		return false
	});
	/*$('a.next, a.order, a.more').click(
		function(){ $(this).css('background-position', '0 bottom'); return false}
		function(){ $(this).css('background-position', '0 0'); return false}
*/
	$('a.more').mousedown(function(){
		$(this).css({'background-position': '0 bottom', 'line-height': '60px'});
	}).mouseup(function(){
		$(this).css({'background-position': '', 'line-height': '57px'});
	});
	$('a.order').mousedown(function(){
		$(this).css({'background-position': '0 bottom', 'line-height': '34px'});
	}).mouseup(function(){
		$(this).css({'background-position': '', 'line-height': '32px'});
	});
	
	$('a.next').mousedown(function(){
      $(this).css({'background-position': '0 bottom', 'line-height': '44px'}); 
    }).mouseup(function(){
      $(this).css({'background-position': '', 'line-height': '42px'}); 
    });

	$('a[rel=logos]').colorbox();
	$('a[rel=layout]').colorbox();
	$('a[rel=icon]').colorbox();
	$('a[rel=ios]').colorbox();
	
	$('.gallery img').hover(
		function(){ $(this).css({ 'padding' : '3px' , 'border' : 'solid 2px #97cbee'})},
		function(){ $(this).css({ 'padding' : '4px' , 'border' : 'solid 1px #cfd2da'})}
		
	);

	$('.field').keyup(function(){
		if ($(this).val() == '') {
			$(this).parent().find('a.del').show();
			$(this).parent().find('a.accept').hide();
		} else {
			$(this).parent().find('a.del').hide();
			$(this).parent().find('a.accept').show();
		}
	});
	$('textarea').keyup(function(){
		if ($(this).val() == '') {
			$('#desc-container a.del').removeClass('hidden');
			$('#desc-container a.accept').addClass('hidden');
		} else {
			$('#desc-container a.del').addClass('hidden');
			$('#desc-container a.accept').removeClass('hidden');
		}
	});
	
	$('form a.next').click(function(){
		$('#main form input').removeAttr('disabled');
		$('#main form').submit();
		return false;
	});
	
	$('#main form').submit(function() {
		if ($(this).is('#contact-form')) {
			// contact form submission
			$.post('/contact.php', $('#contact-form').serialize(), function(msg) {
				if (msg == 'OK') {
					$('#contact-form').parent().html('<span class="thanks">Thank You for Your Message!</span>');
				} else {
					alert('Error, please correct the form and try again.');
				}
			});
			return false;
		} else {
			$('a.del, a.accept').addClass('hidden');
			var valid = true;
			var description = $('#description').val();

			if ($('#fname').val().length == 0) {
				$('#fname').focus();
				$('#fname').siblings('a.del').removeClass('hidden');
				valid = false;
			}
			if ($('#email').val().length == 0) {
				$('#email').focus();
				$('#email').siblings('a.del').removeClass('hidden');
				valid = false;
			}
			if ($('#description').val().length == 0) {
				$('#description').focus();
				$('#desc-container a.del').removeClass('hidden');
				valid = false;
			}

			// Per product validation:
			var p = $('#product').val();
			if (p ==        'logo') {
				if ($('input[name=nlogos]').val() == 0) {
					$('input[name=nlogos]').parents('.l-fld').find('a.del').removeClass('hidden');
					valid = false;
				}
			} else if (p == 'psd-layout') {
				if ($('input[name=mainpages_qty]').val() == 0) {
					$('input[name=mainpages_qty]').parents('.l-fld').find('a.del').removeClass('hidden');
					valid = false;
				}
				if ($('input[name=subpages_qty]').val() == 0) {
					$('input[name=subpages_qty]').parents('.l-fld').find('a.del').removeClass('hidden');
					valid = false;
				}
			} else if (p == 'icon-design') {
				/* no extra validation here */
			} else if (p == 'ios') {
				/* no extra validation here */
			} else if (p == 'psd-to-html') {
				/* no extra validation here */
			}
			return valid;
		}
	});
});

function start_slides( item, time, callback ) {
	var all_items = $( item ).length;
	if ( all_items < 1 ) return;
	var zindex = all_items;
	var i=0;
	$( item ).each( function(){
		$( this ).css( { 'z-index': zindex } );
		if ( i > 0 ) $( this ).hide();
		zindex--;
		i++;
		var img = new Image();
		img.src = $( this ).attr('src');
	});
	
	var index = 0;
	var next;
	var time = time || 5000;
	var callback = callback || function(){};
	
	var interval = window.setInterval( function(){
		if ( index == all_items ) index = 0;
		next = index + 1;
		
		if ( next == all_items ) next = 0;
		
		$( item + ":eq(" + index + ")" ).fadeOut(1200);
		$( item + ":eq(" + next + ")" ).fadeIn(1200, function(){
			callback( next );
		});
		
		index++;
	}, time);
}

function pageLoaded() {
	var w1 = $('#nav').width();
	var w2 = $('#nav ul').width();
	var l1 = $('#nav li').length;
	var p1 = w1 - w2;
	var p2 = p1/l1/2;
	
	$('#nav a').css({
	paddingLeft : p2
	});
	$('#nav li:not[".home"]:not[".email"] a span').css({
	paddingRight : p2
	});
	$('#nav li.home a strong').css('margin-right' , p2);
	$('#nav li.email a strong').css('margin-right' , p2);
	$('#nav li.email a span').css('padding-right' , 0);
	$('#nav li.home a span').css('padding-right' , 0);
}
function calculate_delivery() {
	if ($('#delivery').length > 0) {
		var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
		var home = $('input[name=home_page_qty]').val();
		var subs = $('input[name=sub_page_qty]').val();
		var wp = $('#wordpress').val();
		if (wp == 'Yes-250') {
			wp = 2;
		} else {
			wp = 0;
		}
		var days = parseInt(home) + parseInt(subs) + parseInt(wp);
		var current_date = new Date();
		
		var temp_date = new Date();
		var i = 0;
		var days_to_add = 0;
		while (i < (days)){
			temp_date = new Date(current_date.getTime() + (days_to_add*24*60*60*1000));
			//0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
			if ((temp_date.getDay() != 0) && (temp_date.getDay() != 6)){
				i+=1;
			}
			days_to_add += 1;
		}
		var delivery_day = new Date(current_date.getTime() + days_to_add*24*60*60*1000);
		$('#delivery').val(months[delivery_day.getMonth()] + ' ' + num_abbrev_str(delivery_day.getDate()) + ' - ' + days + ' Business Days');
	}
}

function num_abbrev_str(num) {
	if (typeof num == 'number') {
		num = num.toString()
	}
	var len = num.length, last_char = num.charAt(len - 1), abbrev
	if (len == 2 && num.charAt(0) == '1') {
		abbrev = 'th'
	} else {
		if (last_char == '1') {
			abbrev = 'st'
		} else if (last_char == '2') {
			abbrev = 'nd'
		} else if (last_char == '3') {
			abbrev = 'rd'
		} else {
			abbrev = 'th'
		}
	}
	return num + abbrev
}

function spam_protection() {
	var n1 = Math.floor(Math.random()*11);
	var n2 = Math.floor(Math.random()*11);
	var total = n1 + n2;
	$('#token').val(total);
	$('.sum1').html(n1);
	$('.sum2').html(n2);
}
