// JavaScript Document
(function($) {
	$.fn.ITSailVideoList = function(){
		var oMain = $(this);
		var oDd = $("div.player dl dd", oMain);
		var oLi = $("ul.list li", oMain);
		
		oLi.click(function(){
			oLi.removeClass('on');
			oDd.hide();
			
			$(this).addClass('on');
			oDd.eq($(this).index()).show();
		});
		
		
		oLi.eq(0).addClass('on');
		oDd.eq(0).show();
	};
	
	$.fn.ITSailFaqSlider = function(){
		var oMain = $(this);
		var oItem = $("div.item", oMain);
		var oClass;

		oItem.eq(0).removeClass('on');
		
		oItem.click(function(){
			oClass = $(this).attr('class');
			oItem.removeClass('on');
			$(this).addClass(oClass).toggleClass('on');
		});
	};
})(jQuery);

$(function(){
	//width
	var footer_wrap = $('#footer');
	function findDimensions() {
		var minWidth = $(window).width();
		if(minWidth <= 1226) minWidth = 1226;
		footer_wrap.width(minWidth);
		$('body').width(minWidth);
	}
	$(window).resize(findDimensions);
	findDimensions();
	
	//player
	$('.video-list').ITSailVideoList();
	
	//faq slider
	$('.faq-list').ITSailFaqSlider();
	
	var zips_cost = 1.99;
	$('#btn_calculator').click(function(){
		var num_pieces = $('#num_pieces').val();
		var total_cost = $('#total_cost').val();
		
		if(num_pieces.length == 0 || total_cost.length == 0) {
			alert('Please input the step 1 and step 2 both');
			return false;
		}
		
		if(num_pieces < 1) {
			alert('Please input a number bigger than 0');
			return false;
		}
		
		var avg_cost = Math.ceil(total_cost / num_pieces * 100) / 100;
		if(zips_cost > avg_cost) {
			alert('We are unable to provide savings given the current figures.\nTry ZIPS Dry Cleaners for quality dry cleaning services at one low price.');
			return false;
		}
		
		var zips_total = zips_cost * num_pieces;
		var zips_savings = total_cost - zips_total;
		var zips_savings_year = Math.round(zips_savings * 52);
		var zips_savings_percent = Math.ceil((zips_savings / total_cost) * 100);
		
		$('#cal1').text('$' + avg_cost);
		$('#cal2').text(num_pieces);
		$('#cal3').text('$' + total_cost);
		$('#cal4').text('$1.99');
		$('#cal5').text(num_pieces);
		$('#cal6').text('$' + zips_total);
		$('#cal7').text(zips_savings_percent + '%');
		$('#cal8').text('$' + zips_savings_year + '.00');
	});
});

function RefreshCaptcha() {
	$("img#captcha_img").attr('src', 'index.php?module=captcha&rnd=' + Math.random());
}
function itsail_form_er(form_id) {
	var str = $("#itsail_form_" + form_id).serialize() + '&';
	
	var Phone = $("input[name='frm[Phone]']");
	if(Phone.val().length != 10) {
		alert('Please enter the correctly phone number with 10 digits');
		Phone.focus();
		return false;
	}
	
	if(form_id == 1) {
		var OtherPhone = $("input[name='frm[Other Phone]']");
		if(OtherPhone.val().length == 0) {
			str = str.replace('frm%5BOther+Phone%5D=&', '');
		} else if(OtherPhone.val().length != 10) {
			alert('Please enter your phone number with area code');
			OtherPhone.focus();
			return false;
		}
	}
		
	if(str.indexOf('=&') != -1) {
		alert('Please enter all fields.');
		return false;
	}

	var captcha = $("input[name='frm[captcha]']");
	if(captcha.val().length != 4) {
		RefreshCaptcha();
		alert('Please enter the correctly security captcha');
		captcha.focus();
		return false;
	}

	var er_form_img = $("#er_form_img");
	er_form_img.attr("src", "image/action_loading.gif");
	$.post('index.php', str, function(data){
		if(data.substring(0, 8) == 'success:') {
			er_form_img.attr("src", "image/action_accept.gif");
			if($("input[name='suc_itemid']").val().length > 0) window.location.href = "index.php?itemid=" + $("input[name='suc_itemid']").val();
		} else {
			er_form_img.attr("src", "image/action_stop.gif");
			alert(data);
		}
	}, 'text');
}

