// JQUERY CUSTOM COMMANDS
$(function() {
	
	// rounded corners
	$('.rounded').append('<div class="nw"><\/div><div class="ne"><\/div><div class="se"><\/div><div class="sw"><\/div>');
	
	// mainMenu (temp)
	$('#mainMenu a').click(function() {
		$('#mainMenu li.active').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	
	// headerCart
	$('#headerCart').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			window.location = $(this).find('a.more').attr("href");
		}
	});
	
	// homeVisual
	$('#homeVisual div.frame').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			window.location = $(this).parent().find('a.imgLink').attr("href");
		}
	});
	
	// relatedSlider
	$('.relatedSlider .info').wrapInner('<div class="content"><\/div>').append('<div class="bottom"><\/div>');
	
	// reviews
	$('ul.reviews li:odd').addClass('odd');
	$('ul.reviews li:even').addClass('even');
	
	// DETAIL orderBox (1/2)
	$('div.orderChoice div.choice li.type').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
		    $('input:radio', this).attr('checked', 'checked');
		    $('input:radio', this).trigger('click');
		}
		orderChoice();
	});
	// select first on default
	$('div.addBox li.item input:first').trigger('click');
	orderChoice();
	
	// DETAIL addBox (1/2)
	$('div.addBox li.item').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			$('input:radio', this).attr('checked', 'checked');
		    $('input:radio', this).trigger('click');
		}
		addChoice();
	});
	addChoice();
	
	// cardChoice (1/2)
	$('#cardChoice .listProducts li.item').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			$('input:radio', this).attr('checked', 'checked');
			$('input', this).trigger('click');
		}
		cardChoice();
	});
	cardChoice();
	
	// dateChoice (1/2)
	$('#dateSelector .week li:not(.disabled)').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			$('input:radio', this).attr('checked', 'checked');
			$('input', this).trigger('click');
		}
		dateChoice();
	});
	dateChoice();
	
	// checkoutPayment (1/2)
	$('#checkoutPayment li.check').click(function(event) {
		if ($(event.target).is(":not('a, input, label')") ) {
			$('input:radio', this).attr('checked', 'checked');
			$('input', this).trigger('click');
		}
		checkoutPayment();
	});
	checkoutPayment();
	
	// FAQ
	// Hide all answers
	$('ol.faqList div.answer').hide();
	// Show and hide answers
	$('ol.faqList li').click(function() {
		$('ol.faqList li').not(this).removeClass('active').find('div.answer').slideUp(200);
		$(this).toggleClass('active').find('div.answer').slideToggle(200); // Toggle this answer
	});
	
	
	// FORMS
	// Focus
	$('input.textfield, textarea.textarea').focus(function() {
		$(this).parent().addClass('focus');
	});
	// Clicking on hint
	$('div.input div.hint').not('div.disabled div.hint').click(function() {
		$(this).parent().addClass('focus').find('input');
	});
	$('input.textfield, textarea.textarea').each(function() {
		// Empty filled fields on blur
		$(this).blur(function() {
			$(this).parent().removeClass('focus');
			if ($(this).val() != "") {
				$(this).parent().addClass('filled');
			} else {
				$(this).parent().removeClass('filled');
			};
		});
		// Empty filled fields on load
		if ($(this).val() != "") {
			$(this).parent().addClass('filled');
		} else {
			$(this).parent().removeClass('filled');
		};
	});
	
	/* disabled */
	$('a.disabled').click(function(){
		return false;
	});
	
	/* EQUALHEIGHTS (2/2) */
	equalHeight($("ul.list li.item .head"));
	
	
	// FANCYBOX RESIZE (1/2)
	$('#page_iframe').each(function() {
		fancyResize();
	});

}); // end function


/* EQUALHEIGHTS (1/2) */
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

// DETAIL orderBox (2/2)
function orderChoice() {
	$('div.orderChoice div.choice li.type input').each(function() {
		if ( $(this).is(':checked') ) {
			$(this).parents('li.type').addClass('selected');
		} else {
			$(this).parents('li.type').removeClass('selected');
		}
	});
	if ( $('div.orderChoice div.choice input').is(':checked') ) {
		$('div.orderChoice div.result div.hint').hide();
		$('div.orderChoice div.result div.cart').show();
	} else {
		$('div.orderChoice div.result div.hint').show();
		$('div.orderChoice div.result div.cart').hide();
	}
}

// addBox (2/2)
function addChoice() {
	$('div.addBox li.item input').each(function() {
		if ( $(this).is(':checked') ) {
			$(this).parents('li.item').addClass('selected');
		} else {
			$(this).parents('li.item').removeClass('selected');
		}
	});
}

// cardChoice (2/2)
function cardChoice() {
	$('#cardChoice .listProducts li.item input').each(function() {
		if ( $(this).is(':checked') ) {
			$(this).parents('li.item').addClass('selected');
		} else {
			$(this).parents('li.item').removeClass('selected');
		}
	});
}

// dateChoice (2/2)
function dateChoice() {
	$('#dateChoice .week li input').each(function() {
		if ( $(this).is(':checked') ) {
			$(this).parents('li').addClass('selected');
		} else {
			$(this).parents('li').removeClass('selected');
		}
	});
}

// checkoutPayment (2/2)
function checkoutPayment() {
	$('#checkoutPayment li.check input').each(function() {
		if ( $(this).is(':checked') ) {
			$(this).parents('li').addClass('selected');
		} else {
			$(this).parents('li').removeClass('selected');
		}
	});
}


// FANCYBOX RESIZE (2/2)
function fb_resize(w, h) {
  if (w > 0 || h > 0) {
    if (w > 0) $('#fancybox-content').css({ width: w+"px"});
    if (h > 0) $('#fancybox-content').css({ height: h+"px"});
    $.fancybox.resize();
  }
}
function fb_close_any() {
  window.top.eval('$.fancybox.close()');
}
function fb_resize_any(w, h) {
  w = (w > 0) ? w : 0;
  h = (h > 0) ? h : 0;
  window.top.eval('fb_resize('+w+','+h+')');
}
// Set maxHeight or contentHeight to fancybox
function fancyResize() {
	setTimeout(function() {
		var windowHeight = ($(window.parent).height() * 0.8);
		var contentHeight = ($('body').height() + 20);
		if (windowHeight > contentHeight) {
			var frameHeight = contentHeight;
		} else {
			var frameHeight = windowHeight;
		}
		var frameHeight = Math.floor(frameHeight);
		//alert(frameHeight);
		fb_resize_any('auto',frameHeight);
	}, 500);
}
