$(function() {
	$("h3.toggle").click(function() {
		$(this).next("div").slideToggle("slow");
		$(this).children("span").toggleClass("down_arrow");
	});
	$(".list_strip a").click(function() {
		$("#open_video").click();
	});

	/* CALCULATOR */
	var ALL_DATA = null,APPLY = 0, RELATION = 0, btnColor = null, btnClear = null, appl = null;

	init = function() {
		$('#txtPrice').attr('disabled', true);

		$('#txtPrice').keypress(function(event) {
			var e = event || evt;
			var charCode = e.which || e.keyCode;
			if (charCode >= 48 && charCode <= 57 || (charCode == 8 || charCode == 37 || charCode == 39 || charCode == 46)) {
				$('#btnCancel').addClass('hidden');
				$('#btnApply').removeClass('hidden');
				if (!appl) {
					setTimeout(function() {
						toggle_applay(true)
					}, 500);
				}
			}
			else {
				return false;
			}
		});

		$('.btnColor').click(function() {
			$('.btnColor').removeClass('clicked');
			if (btnColor) btnColor.attr('src', '/images/calc/' + btnColor.val() + '_blue.png');
			btnColor = $(this);
			$(this).attr('src', '/images/calc/' + $(this).val() + '_gray.png');
			$(this).addClass('clicked');
			if (APPLY) calculate_price();
			$('#txtColor').text(this.value);
			calculate_price();
		});

		$('.btnClear').click(function() {
			$('.btnClear').removeClass('clicked');
			if (btnClear) btnClear.attr('src', '/images/calc/' + btnClear.val() + '_blue.png');
			btnClear = $(this);
			$(this).attr('src', '/images/calc/' + $(this).val() + '_gray.png');
			$(this).addClass('clicked');

			if (APPLY) calculate_price();
			$('#txtClearity').text(this.value);
			calculate_price();
		});

		$('#btnCancel').click(function() {
			reset();
		});
		$('#btnCancel').mouseover(function() {
			$(this).attr('src', '/images/calc/clean_up.png');
		});
		$('#btnCancel').mouseout(function() {
			$(this).attr('src', '/images/calc/clean_down.png');
		});

		$.getJSON('/calculator/calc.php', {action:'load_data'}, function(data) {
			ALL_DATA = data;
		});

		$('#btnApply').click(function() {
			APPLY = 1;
			$('#btnApply').addClass('hidden');
			clearTimeout(appl);
			$('#btnCancel').removeClass('hidden');
			$('#msg_start').removeClass('hidden');

			calculate_price(1);
		});

		$('#ruller div').slider({animate:true,max:4,min:0.5,orientation:'vertical',range:'min',step:0.01,value:4,
			start: function(event, ui) {

			},
			stop: function(event, ui) {
				if (APPLY) calculate_price();
				setWeightVal(ui.value);
				calculate_price();
			},
			slide: function(event, ui) {
				setWeightVal(ui.value);
				var w = 4 * ui.value + 8, l = 12 - w / 2;
				$('.ui-slider-handle img').css({'width': w, 'left':l});
			}
		});
		$('.ui-slider-handle').html('<img src="/images/calc/round.png" style="left:0" />');
		$('#arr_top').click(function() {
			$('#ruller div').slider("value", $('#ruller div').slider("value") + 0.01);
			setWeightVal(roundNumber($('#ruller div').slider("value"), 2));
			if (APPLY) calculate_price();
		});
		$('#arr_bottom').click(function() {
			$('#ruller div').slider("value", $('#ruller div').slider("value") - 0.01);
			setWeightVal(roundNumber($('#ruller div').slider("value"), 2));
			if (APPLY) calculate_price();
		});
		$('#calculator .msg_weight').show('normal');

		$("#calculator .block input[type=image]").mouseover(function() {
			$(this).not('.clicked').attr('src', '/images/calc/' + $(this).val() + '_blue.png');
		});
		$("#calculator .block input[type=image]").mouseout(function() {
			$(this).not('.clicked').attr('src', '/images/calc/' + $(this).val() + '_blue_down.png');
		});
	}


	function toggle_applay(flag) {

		if (flag)
			$('#btnApply').attr('src', '/images/calc/applay_up.png');
		else
			$('#btnApply').attr('src', '/images/calc/applay_down.png');
		flag = !flag;
		$('#btnApply').val(appl);
		appl = setTimeout(function() {
			toggle_applay(flag)
		}, 500);
	}

	get_table = function(val) {
		var tbl;
		if (val >= 0.5 && val < 0.7) {
			tbl = 'table_0.5-0.69';
		}
		else if (val >= 0.7 && val < 0.9) {
			tbl = 'table_0.7-0.89';
		}
		else if (val >= 0.9 && val < 1) {
			tbl = 'table_0.9-0.99';
		}
		else if (val >= 1 && val < 1.5) {
			tbl = 'table_1-1.49';
		}
		else if (val >= 1.5 && val < 2) {
			tbl = 'table_1.5-1.99';
		}
		else if (val >= 2 && val < 3) {
			tbl = 'table_2-2.99';
		}
		else if (val >= 3 && val < 4) {
			tbl = 'table_3-3.99';
		}
		else if (val >= 4 && val < 5) {
			tbl = 'table_4-4.99';
		}
		else {
			tbl = null;
		}
		return tbl;
	}

	calculate_price = function(applay) {
		var old_pr = parseFloat($('#txtPrice').val()), weight = parseFloat($('#txtWeight').text()), clearity = $.trim($('#txtClearity').text()), color = $.trim($('#txtColor').text());

		if (isNaN(weight) == true) {
			$('#calculator .msg_weight').show('normal');
		}
		else if (clearity.length == 0) {
			$('#calculator .msg').hide('fast');
			$('#calculator .msg_clear').show('normal');
		}
		else if (color.length == 0) {
			$('#calculator .msg').hide('fast');
			$('#calculator .msg_color').show('normal');
		}
		else {
			var txts = $('#txtPrice');
			if (txts.attr('disabled')) {
				txts.attr('disabled', false);
				$('#calculator .msg').hide('fast');

				$('#calculator .msg_price').show('normal');
				txts.focus();

				txts.addClass("idleField");
				txts.focus(function() {
					$(this).val('');
					txts.removeClass('err');
					$(this).removeClass("idleField").addClass("focusField");
					if (this.value == this.defaultValue) {
						this.value = '';
					}
					if (this.value != this.defaultValue) {
						this.select();
					}
				});
				txts.blur(function() {
					$(this).removeClass("focusField").addClass("idleField");
					if ($.trim(this.value) == '') {
						$(this).addClass('err');
						this.value = (this.defaultValue ? this.defaultValue : '');
					}
				});
			}
			else {

				if (!old_pr) {

				}
				else {
					var tbl = get_table(weight);
					if (tbl) {
						$('#calculator .msg').hide('fast');
						if (typeof applay == 'undefined' && RELATION) {
							resetMsgs();
							$('#txtPrice').val(Math.round(ALL_DATA[tbl][color][clearity] * weight / RELATION));
						}
						else if (applay == 1) {   /*The first time count*/

							RELATION = ALL_DATA[tbl][color][clearity] * weight / old_pr;
						}
					}
					else {
						$('#calculator .msg_weight').show('normal');
					}
				}
			}
		}
	};
	reset = function() {
		APPLY = RELATION = 0;
		$('.btnClear').css('border', 'none');
		$('#txtClearity').text('');
		$('.btnColor').css('border', 'none');
		$('#txtColor').text('');
		$('#ruller div').slider("value", 4);
		$('#txtWeight').text('');
		$('#txtPrice').val('');
		$('#txtPrice').attr('disabled', true);
		$('.ui-slider-handle img').css({'width': 24, 'left':0});
		if (btnClear) btnClear.attr('src', '/images/calc/' + btnClear.val() + '_blue.png');
		if (btnColor) btnColor.attr('src', '/images/calc/' + btnColor.val() + '_blue.png');
		$('.btnClear').removeClass('clicked');
		$('.btnColor').removeClass('clicked');
		btnColor = btnClear = appl = null;
		resetMsgs();
		$('#calculator .msg_weight').show('normal');
	};

	resetMsgs = function() {
		$('#calculator .msg').hide('fast');
	};

	roundNumber = function(rnum, rlength) {
		return Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
	};

	setWeightVal = function(val) {
		var diff = val - parseInt(val);
		if (diff == 0) {
			val = val + '.00';
		}
		else if (parseInt(diff * 10) - diff * 10 == 0) {
			val = val + '0'
		}
		$('#txtWeight').text(val);
	};

	init();
});

function addFriend() {
	var maxFriendLineId = parseInt($("#tblFriends tbody tr:last td.tdname input").attr('id').replace("MasterClass_friendNames", ""));
	if (isNaN(maxFriendLineId)) {
		maxFriendLineId = -1;
	}
	maxFriendLineId++;
	$('<tr><td class="tdlabel"><label for="MasterClass_friendNames' + maxFriendLineId + '" class="bold">*</label></td><td class="tdname"><input type="text" id="MasterClass_friendNames' + maxFriendLineId + '" name="MasterClass[friendNames][' + maxFriendLineId + ']" class="text" minlength="2"></td><td class="tdemail"><label for="MasterClass_friendEmails' + maxFriendLineId + '" class="bold">*</label> <input type="text" id="MasterClass_friendEmails' + maxFriendLineId + '" name="MasterClass[friendEmails][' + maxFriendLineId + ']" class="text"></td><td><input type="button" class="button-remove" onclick="RemoveRow(this)"></td></tr>').appendTo("#tblFriends tbody");
}

function RemoveRow(removeButton) {
	$(removeButton).parent().parent().remove();
}

function submitMasterclass() {
	$.post($("#fRegistration").attr("action"),
			$("#fRegistration").serialize(),
			function(data) {
				$('label[for^="MasterClass_"]').removeAttr('class');
				if (data["status"] == "error") {
					$("[name=captchaImage]").click();
					for (key in data["errors"]) {
						if (key == "friendNames" || key == "friendEmails") {
							for(key2 in data["errors"][key])
							{
								$('label[for="MasterClass_' + key + data["errors"][key][key2]["index"] + '"]').addClass('error');
							}

						}
						else {
							$('label[for="MasterClass_' + key + '"]').addClass('error');
						}
					}
				}
			}
	);
}
