// default.js

var genNumber = 0;
var seasonalNumber = 0;
var payNumber = 0;

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

$(function(){
		   
	//
	$.ajaxSetup({ 
			scriptCharset: "utf-8"
	});
	
	$(document).pngReplace();
	
	// calendar
	$.fn.qtip.styles.cal = { 
	   width: 280,
	   textAlign: 'left',
	   border: {
		  width: 1,
		  radius: 2,
		  color: '#cccccc'
	   },
	   tip: 'bottomLeft',
	   name: 'light'
	}
	
	$('.event>a').each(function(){
		$(this).qtip({
		  content: $(this).attr('meta'),
		  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
		  style: 'cal'
		});
	});
	
	$('#genItem.template').hide();
	$('#seasonalItem.template').hide();
	
	if($.browser.msie){
		//$('#seasonalBtn').html('&nbsp;');	
	}
	
	addRequest();
	addSeasonalRequest();
	
	$('.photos>li>a').fancybox();
	
	$('.custom a').fancybox();
	
});

function addRequest(){

	$('#genList').append($('#genItem.template').clone());
	
	genNumber++;
	
	$('#genList #genItem.template').removeClass('template').attr('id', 'genItem_' + genNumber).fadeIn('slow').find('input').each(function(){
		$(this).attr('name', $(this).attr('name') + '_' + genNumber);
	});
	
	$('form').find('input:hidden[name^=amount]').each(function(i){	
		
		var totalPrice;
		
		if(genNumber <= 3){
			perPrice = 20 / genNumber;
			if(i == 2){ 
				perPrice = 6.66;
			}
			totalPrice = 20;
		} else if(genNumber <= 6){
			perPrice = 30 / genNumber;	
			totalPrice = 30;
		} else {
			perPrice = 5;
			totalPrice = perPrice * genNumber;
		}
		
		$(this).val(sprintf('%01.2f', perPrice));
		
		$('#totalPrice').html('$ ' + sprintf('%01.2f', totalPrice));
	});

	return false;

}

function deleteRequest(){
	
	var totalPrice;
	
	if(genNumber > 1){
		
		$('#genItem_' + genNumber).remove();
	
		genNumber--;
	
		$('form').find('input:hidden[name^=amount]').each(function(i){	
			if(genNumber <= 3){
				perPrice = 20 / genNumber;
				if(i == 2){ perPrice = 6.66 }
				totalPrice = 20;
			} else if(genNumber <= 6){
				perPrice = 30 / genNumber;	
				totalPrice = 30;
			} else {
				perPrice = 5;
				totalPrice = perPrice * genNumber;
			}
			
			$(this).val(sprintf('%01.2f', perPrice));
			
			$('#totalPrice').html('$ ' + sprintf('%01.2f', totalPrice));
		});
		
	} else {
		
		$('#genItem_1').find('input:text').each(function(){										  
			$(this).val('');
		});	
		
		$('#genItem_1').find('input:hidden[name^=amount]').each(function(){										  
			$(this).val(30);
		});	
	}
	
	return false;
	
}

function addSeasonalRequest(){

	$('#seasonalList').append($('#seasonalItem.template').clone());
	
	seasonalNumber++;
	
	$('#seasonalList #seasonalItem.template').removeClass('template').attr('id', 'seasonalItem_' + seasonalNumber).fadeIn('slow').find('input,select').each(function(){
		$(this).attr('name', $(this).attr('name') + '_' + seasonalNumber);
	});
	
	computeSeasonalTotal();

	return false;

}

function deleteSeasonalRequest(){
	
	if(seasonalNumber > 1){
		
		$('#seasonalItem_' + seasonalNumber).remove();
	
		seasonalNumber--;
	
		computeSeasonalTotal();
		
	} else {
		
		$('#seasonalItem_1').find('input:text').each(function(){										  
			$(this).val('');
		});	
							   
	}
	
	return false;
	
}

function computeSeasonalTotal(obj){
	
	if(obj){
		tempStr 	= obj.attr('name');
		tempStr 	= tempStr.substring(7);
		
		tempName	= obj.find('option:selected').text();
		tempName	= tempName.split(' - ');
		
		$('input[name=item_number_' + tempStr + ']').val(obj.find('option:selected').attr('meta'));
		$('input[name=item_name_' + tempStr + ']').val(tempName[1]);
	}
	
	var tempTotal = 0;
	
	$('#seasonalList select[name^=amount]').each(function(){
		tempPrice = $(this).val();
		tempPrice = tempPrice.replace(/\$/, '');
		tempTotal += parseFloat(tempPrice);
	});
	
	$('#totalPrice').html('$ ' + sprintf('%01.2f', tempTotal));
}

// sprintf functions
function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ('');
    f = f.substring(m[0].length);
  }
  return o.join('');
}
