$.ajaxSetup ({  
	cache: false  
});

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

String.prototype.replaceAll = function(
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
	// Relace out the current instance.
	strText = strText.replace( strTarget, strSubString )
	 
	// Get the index of any next matching substring.
	intIndexOfMatch = strText.indexOf( strTarget );
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}

var slide_t;

var rootUrl = 'http://www.slicejack.com/';

$(document).ready(function() {
	
	//	HEADER
	
	$('#sub-header .services li a').hover(
		function() {
			$(this).children('span').css({'display':'block'});
			$(this).children('span').animate(
				{
					'margin-top': '-=20px'				
				}
			);
			$(this).animate(
				{
				    opacity: '1'
				}
			);
		}
		,
		function() {
			$(this).animate(
				{
				    opacity: '0.5'
				},
				function () {
					$(this).children('span').css({'display':'none'});
					$(this).children('span').css({'margin-top':'-30px'});
				}
			);
		},
		500
	);
	
	
	
	
	//	SLIDER
	
	$('#sub-header .slider .info-nav li a').click(
		
		function () {
			
			clearTimeout(slide_t);
			
			if ($(this).hasClass('active')) return false;
			
			var indexOld = $(this).parent().parent().children('li').children('a').index( $(this).parent().parent().children('li').find('.active'));
			var index = $(this).parent().parent().children('li').children('a').index($(this));
			var sign = '+';
			if (index > indexOld) sign = '-';
			
			$(this).parent().parent().children('li').find('.active').removeClass('active');
			$(this).addClass('active');
			
			$('#sub-header .slider .slider-content-wrapper').children('.slider-content:eq(' + indexOld + ')') .fadeOut(500, function () {
				$('#sub-header .slider .slider-content-wrapper').children('.slider-content:eq(' + index + ')') .fadeIn();
			});
			/*
			$('#header .slider .slider-content-wrapper').animate({
				left: sign + '=' + (Math.abs(index - indexOld) * 940) + 'px'
			},
			500
			);
			*/
			
			slide_t = setTimeout('do_slide()', 6000);
			
		}
	
	);
	
	//	TESTIMONIALS
	
	// setTimeout('testimonials()', 5000);
	// slide_t = setTimeout('do_slide()', 10000);
	
	/*
	$('#sidebar .more a.show-more').click(
	
		function () {
			
			$('#sidebar blockquote').show();
			$('#sidebar .more').hide();
			
		}
	
	);
	*/
	
	//	CONTACT - FAQ
	
	$('#main-content .faq ul li h4 a').click(
	
		function () {
		
			$('#main-content .faq ul li p').slideUp();
			
			$(this).parent().parent().children('p').slideDown();
			
			return false;
			
		}
	
	);
	
		//	HOVER
	
	$('.hover-effect').hover(
		function() {
			$(this).find('.hover').fadeIn();
		},
		function() {
			$(this).find('.hover').fadeOut();
		}
	);
	
	//	ORDER - FILE
		
	$("input.file_s").filestyle({ 
      image: rootUrl + "wp-content/themes/slicejack/images/buttons/browse.png",
      imageheight : 34,
      imagewidth : 74,
      width : 142
    });
    
    /*
    $(".files div").css(
	    {
	    	cursor: 'pointer'
	    }
    )
    */
    
    //	ORDER - BADGE
    
    $("#choose-service").click(
    
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		$("#price-badge").animate({
    			top: '140px'
    		},
    		2000);
    		return true;
    	}
    	
    )
    
    $("#number-of-pages").click(
    
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		$("#price-badge").animate({
    			top: '200px'
    		},
    		2000);
    		return true;
    	}
    	
    )
    
    $("#turnaround-time").click(
    
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		$("#price-badge").animate({
    			top: '260px'
    		},
    		2000);
    		return true;
    	}
    	
    )
    
    $(".files input").change(
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		price_badge_calc();
    		$("#price-badge").animate({
    			top: '520px'
    		},
    		2000);
    	}
    )
    
    $(".link input").change(
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		price_badge_calc();
    		$("#price-badge").animate({
    			top: '520px'
    		},
    		2000);
    	}
    )
    
    $("#comments").click(
    	function() {
    		if (!$("#price-badge").hasClass('order')) { $("#price-badge").addClass('price'); }
    		price_badge_calc();
    		$("#price-badge").animate({
    			top: '520px'
    		},
    		2000);
    	}
    )

    
    // IFrame Size Fix
    $(window).resize(function() {
        calcHeight();
      }).load(function() {
        calcHeight();
      });
      
      setTimeout('calcHeight()', 3000);
      setTimeout('calcHeight()', 6000);
      setTimeout('calcHeight()', 9000);
      setTimeout('calcHeight()', 12000);
      
      $(['http://www.slicejack.com/wp-content/themes/slicejack/images/headings/focus_on_design.png','http://www.slicejack.com/wp-content/themes/slicejack/images/headings/reliable_and_professional.png','http://www.slicejack.com/wp-content/themes/slicejack/images/headings/three_step.png']).preload();
	
      setTimeout(function() { testimonials(); }, 7000 );
      
});

var calcHeight = function() {
	//var headerDimensions = $('#header').height() + $('#sub-header').height();
	var headerDimensions = $('#sub-header').height();
	$('#preview-xhtml div div').height($(window).height() - headerDimensions - 35);
	$('#preview-css div div').height($(window).height() - headerDimensions - 35);
	$('#preview-design').height($(window).height() - headerDimensions);
}


function price_badge_calc() {
	
	var calc;
	
	//	Services
	
	switch($("#choose-service").val())
	{
		case 'PSD to (X)HTML':
		  calc = 198;
		  break;
		case 'PSD to Mobile':
		  calc = 99;
		  break;
		case 'PSD to Tablet':
		  calc = 198;
		  break;
		case 'PSD to WordPress CMS':
		  calc = 298;
		  break;
		case 'PSD to Joomla CMS':
		  calc = 298; 
		  break;
		case 'PSD to Email':
		  calc = 198; 
		  break;
	}
	
	// Num of pages
	
	calc = calc + (($("#number-of-pages").val() - 1) * 90);
	
	// Turnaround time
	
	switch($("#turnaround-time").val())
	{
		case 'Express ( 1-3 days )':
			calc = calc + (calc / 2);
			break;
	}
	
	return '$' + calc;
	
}


function testimonials() {

	$.each($('div.testimonial'), function() {
		if ($(this).find('blockquote').length < 2) return false;
		var index = $(this).find('blockquote').index($(this).find('blockquote:visible'));
		$(this).find('blockquote:visible').fadeOut();
		if ($(this).find('blockquote').length > (index+1)) {
			index++;
		} else {
			index=0;
		}
		$(this).find('blockquote:eq(' + index + ')').fadeIn();
	});
	
	setTimeout(function() { testimonials(); }, 7000 );
	
}


function do_slide() {

	var indexOld = $('#sub-header .slider .info-nav li').children('a').index( $('#sub-header .slider .info-nav li').find('.active'));
	var index = indexOld + 1;
	if (index > 2) index = 0;
	var sign = '+';
	if (index > indexOld) sign = '-';
	
	$('#sub-header .slider .info-nav li').find('.active').removeClass('active');
	$('#sub-header .slider .info-nav li a:eq(' + index + ')').addClass('active');
	
	$('#sub-header .slider .slider-content-wrapper').children('.slider-content:eq(' + indexOld + ')') .fadeOut(500, function () {
		$('#sub-header .slider .slider-content-wrapper').children('.slider-content:eq(' + index + ')') .fadeIn();
	});
	
	/*
	$('#header .slider .slider-content-wrapper').animate(
		{
			left: sign + '=' + (Math.abs(index - indexOld) * 940) + 'px'
		},
		500
	);
	*/
	
	slide_t = setTimeout('do_slide()', 10000);

}

function showPopup(p,t) {
	
	var h = screen.height;
    var w = screen.width;
	
	window.open(rootUrl + 'wp-content/themes/slicejack/portfolio_view.php?type=' + t + '&p=' + p , '_blank', 'height=' + h + ', width=' + w + ',scrollbars=0,location=0,toolbar=0,status=0,menubar=0');
	return false;
	
}




//	ORDER JS

$(document).ready(function() {

	$('#order-send').click(

		function () {
		
			$('#order-send').attr("disabled", true);
			$('#error').css({'display':'none'});
			$('#order-loading').css({'display':'block'});
			
			$('#order-amount').val( price_badge_calc() );
			
			$('#order-form').ajaxSubmit( {success: OrderSent, dataType:'json'} );
			
			return false;
			
		}
		
	);

});

function OrderSent(responseText) {
	
	// alert(responseText);

	switch (responseText.result) {
		case 'true':
			$('#order-loading').fadeOut(500);
			$('#success').fadeIn(500);
			break;
		case 'false':
			$('#order-loading').fadeOut(500);
			$('#error').children('div').children('ul').html(responseText.fields.replaceAll('.', '<li>').replaceAll(',', '</li>'));
			$('#error').fadeIn(500);
			$('#order-send').attr("disabled", false);
			break;
	}
	
}

//	CONTACT JS

function SendContact() {
	
	$('#contact-send').attr("disabled", true);
	$('#error').css({'display':'none'});
	$('#contact-loading').css({'display':'block'});
	
	$('#contact-form').ajaxSubmit( {success: ContactSent, dataType:'json'} );
	
	return false;

}

function ContactSent(responseText) {
	
	// alert(responseText);
	
	switch (responseText.result) {
		case 'true':
			$('#contact-loading').fadeOut(500);
			$('#success').fadeIn(500);
			break;
		case 'false':
			$('#contact-loading').fadeOut(500);
			$('#error').children('div').children('ul').html(responseText.fields.replaceAll('.', '<li>').replaceAll(',', '</li>'));
			$('#error').fadeIn(500);
			$('#contact-send').attr("disabled", false);
			break;
	}
	
}
