// UI SCRIPTS


// VERTICALLY ALIGN FUNCTION
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = Math.ceil((ph-ah) / 2);
	$(this).css('margin-top', mh);
	});
};
})(jQuery);


//IE Test Function
var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());


$(document).ready(function() {

	// Accordion Menu
	$(".prod-cat-nav li").click(function() {
		if ($(this).hasClass("closed")) {
			$(".prod-cat-nav li.open").addClass("closed");
			$(".prod-cat-nav li.open").removeClass("open");
			$(this).addClass("open");
			$(this).removeClass("closed");
		}
	});

	// For 3rd Level in RIO Products (Fly Lines / Leaders / Tippets)
	/*
	$(".prod-cat-nav li ul li").click(function() {
		if ($(this).hasClass("closed_third")) {
			$(".prod-cat-nav li ul li.open_third").addClass("closed_third");
			$(".prod-cat-nav li ul li.open_third").removeClass("open_third");
			$(this).addClass("open_third");
			$(this).removeClass("closed_third");
		} else {
			$(".prod-cat-nav li ul li.open_third").addClass("open_third");
			$(".prod-cat-nav li ul li.open_third").removeClass("closed_third");
			$(this).addClass("closed_third");
			$(this).removeClass("open_third");
		}
	});
	*/

	// Accordion Family PDP Charts
	$(".accordion_bar").click(function() {
		if($(this).parent().hasClass("cat_closed")) {
			$("#model_select .cat_open").addClass("cat_closed");
			$("#model_select .cat_open").removeClass("cat_open");
			$(this).parent().addClass("cat_open");
			$(this).parent().removeClass("cat_closed");
		}
	});



	//  Add cart link to item # in header
	$("#cart-display .title-bar span.full").wrap("<a href='cart.php'></a>");

	$('#loginButton').click(function(evt) {
		//console.log(evt);
		evt.preventDefault();
		evt.stopPropagation();

		console.log($('#loginForm'));
		$('#loginForm').submit();
	});

	// PDP Qty Change
	$("input.pdp_qty").change( function() {
		var myAmount = $(this).attr("value");
		var myBuyBtn = $(this).parent().siblings("td").find(".prod_buy_control");
		var myHREF = myBuyBtn.attr("href");
		var myNewHREF = myHREF.split("=");

		myNewHREF[myNewHREF.length - 1] = myAmount;

		var myURL = "";

		for (x in myNewHREF)
		{
			if (x != myNewHREF.length - 1)
				myURL += myNewHREF[x] + "=";
			else
				myURL += myNewHREF[x];
		}

		myBuyBtn.attr("href", myURL);
	});

	// Select Boxes
	//if(ie != 7)
	//	$("select").sb({ fixedWidth: true });

	// CC Exp field size
	$("label.data-required").each(function() {
		if ($(this).text() == "Expiration date (month/year)") {
			var myValues = $(this).parent().siblings(".data-value");
			myValues.find(".selectbox").css("width", "70px");
			myValues.find(".selectbox .display").css("width", "43px");
		}
	});


	var addInputs = $("input[name^='additional_values']");
	var addSelects = $("select[name^='additional_values']");
	var addFields = jQuery.merge(addInputs, addSelects);

	if (addFields && addFields.length) {
		var addSel = $('#additional_values_1');
		if (addSel) {
			addSel.change(function() {
				var idx = ($("#additional_values_1 option").index($("#additional_values_1 option:selected"))+1) * 100;
				addFields.each(function() {
					var dif = parseInt($(this).attr('rel')) - idx;
					var rel = parseInt($(this).attr('rel'));

					if (rel != 10 && rel < 1000) {
						if (idx > 0 && dif >= 0 && dif < 100) {
							$(this).parent().parent().css('display', 'table-row');
							$(this).parent().prev().addClass('data-required');
							$(this).parent().prev().text('*');
						} else {
							$(this).parent().parent().css('display', 'none');
							$(this).parent().prev().removeClass('data-required');
							$(this).parent().prev().text('');
						}
					}
				});
			});
		}

		addFields.each(function() {
			var rel = parseInt($(this).attr('rel'));

			if (rel >= 200 && rel < 1000) {
				$(this).parent().parent().css('display', 'none');
			}
		});
	}
});

$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images

// Normalize Landing/Wall Item Height
	var normHeight = 0;

	$(".landing-subcat").each(function() {
		if( $(this).height() > normHeight)
			normHeight = $(this).height();
	});
	$(".landing-subcat").height(normHeight);
	
	
	// PDP image valign
	$("#pdp-img #product_thumbnail").vAlign();

});
