var url = $(location).attr('href');
if ($.browser.msie && $.browser.version.substr(0,1)<7 && url.substring(url.length-4,url.length) != '/ie6') {
	$(location).attr('href','/ie6');
}

//--- Used to help determine how to apply styles on the Where to Buy page

function getUrlVars()
	{
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}


$(document).ready(function() {


//--- Faceted Search 

	$('.faceted-search-facet > h3').bind('click',function() {
		$(this).parent().toggleClass('open');
	});

	if(($('#content-area .faceted-search-facet--field_product_family_name_c--1').length > 0)) {

		half_facet_items_count = (($('#content-area .faceted-search-facet--field_product_family_name_c--1 .item-list ul li').size()* .5) - 1);
		facet_margin_adjust = '-' + ((half_facet_items_count * 21.5) + 'px');

		$('#content-area .faceted-search-facet--field_product_family_name_c--1 .item-list ul li:gt(' + half_facet_items_count + ')').each(function() {
			$(this).next().addClass('second-half');
		});
	
		$('#content-area .faceted-search-facet--field_product_family_name_c--1 .item-list ul li[class="second-half"]:first').css({'margin-top': facet_margin_adjust});					

		$('#content-area .faceted-search-facet--field_product_family_name_c--1 .item-list ul').css({'padding-bottom' : '22px'});
	}



//--- Eliminates some of the redundant data-entry in the dealer node-add page

	$('.page-node-add-dealer #node-form #edit-title-wrapper input#edit-title').blur(function() {
		var dealertitle = $(this).val();
		$('#edit-field-location-0-name').val(dealertitle);			
	});

	$('.page-node-add-dealer fieldset.location input').blur(function() {

		var dealerval = $(this).val();
		var did = $(this).attr('id');	
		var didupdate;

		if((dealerval).length != 0) {

			switch (did) {
				case 'edit-field-location-0-street':
					didupdate = '#edit-field-address-0-value';
					break;
				case 'edit-field-location-0-city':
					didupdate = '#edit-field-city-0-value';
					break;
				case 'edit-field-location-0-province':
					didupdate = '#edit-field-state-0-value';
					break;
				case 'edit-field-location-0-postal-code':
					didupdate = '#edit-field-zip-0-value';
					break;
				case 'edit-field-location-0-street':
					didupdate = '#edit-field-address-0-value';
					break;	
			}
		$(didupdate).val(dealerval);			
		}		
	});







//--- Used to help determine how to apply styles on the Where to Buy page.

	var check_term = getUrlVars()["distance%5Bpostal_code%5D"];

	if(typeof check_term == 'undefined') {
		$('.view-location-search .view-empty').addClass('hide');
		$('.view-location-search .view-header').addClass('show');
	}
	if(typeof check_term != 'undefined') {
		$('.view-location-search .view-empty').removeClass('hide');
		$('.view-location-search .view-header').removeClass('show');
	}



	if(parent.location.href != location.href) {
		$('body').addClass('translator');
	}

	$('.node-type-products .node-form div.form-checkboxes .form-checkbox').click(function(i) {
		iterate_checks();
	});
	iterate_checks();

	if ( $("body.page-wheretobuy").length > 0 ) {

		$('#edit-city-wrapper').append('<a href="#" id="show-postal">Search by Postal Code</a>');

		zip = $('#edit-distance-postal-code').val();
		country = $('#edit-distance-country').val();
		distance = $('#edit-distance-search-distance').val();
		units = $('#edit-distance-search-units').val();
		if(zip && country && distance && units) {
			$.ajax({
				type: "GET",
				url: "/wheretobuy-ajax/"+zip+"/"+country+"/"+distance+"/"+units,
				data: "",
				success: function(msg){
					$('#content-area').append(msg);
					
				//--- Switches miles to km when appropriate on the Where to Buy page
			
						if(($('#edit-distance-search-units option:selected')).val() == 'km') {
							$('.view-id-location_search.view-display-id-block_1 .views-row .views-field-distance .field-content').each(function() {
								$(this).html($(this).html().replace(' mi', ''))						
								$(this).html(($(this).html()*1.61).toFixed(2));
								$(this).html($(this).html() + " km");
							});
						}																																	
								
					
				}
			});
		} else {
			//alert('here');
			$('.view-location-search .view-empty').removeClass('hide');
		}
	}

	$('#show-postal').mousedown(function() {
		$('.view-location-search').addClass('show');
	});



	$("#block-graff_menu-3 input, #block-graff_menu-4 input").focus(function(srcc) {
			if ($(this).val() == $(this)[0].title) {
				$(this).removeClass("defaultTextActive").val("");
			}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("defaultTextActive").val($(this)[0].title);
		}
	}).blur();


	/* FANCY TOOLTIP ON PRODUCT PAGE */
	$("#product-line-ajax-content").bind('mouseenter',function() {
		$("#product-line-ajax-howto").show('slow');
	});
	$("#product-line-ajax-content").bind('mouseleave',function() {
		$("#product-line-ajax-howto").hide();
	});

});


var decodes = new Array();
decodes[601]  = "field-product-main-photo";
decodes[602]  = "field-finish-abb";
decodes[603]  = "field-finish-abn";
decodes[604]  = "field-finish-acu";
decodes[605]  = "field-finish-nb";
decodes[606]  = "field-finish-orb";
decodes[607]  = "field-finish-ob";
decodes[609]  = "field-finish-pn";
decodes[610]  = "field-finish-sf";
decodes[611]  = "field-finish-sn";
decodes[612]  = "field-finish-ab";
decodes[613]  = "field-finish-au";
decodes[2623] = "field-finish-un";

function iterate_checks() {
	// if(!$('#edit-field-finishes-list-nid-nid-2623').attr('checked') && !$('#edit-field-finishes-list-nid-nid-601').attr('checked')) {
	// 	$('#edit-field-finishes-list-nid-nid-601').attr('checked',true);
	// }
	$('#edit-field-finishes-list-nid-nid-601-wrapper').parent().find('.form-checkbox').each(function(i) {
		code = decodes[$(this).val()];
		if($(this).attr('checked')) {
			$('#edit-'+code+'-0-ahah-wrapper .form-item').show();
		} else {
			//$('edit-field-finish-'+code+'-0-upload').val('');
			$('#edit-'+code+'-0-ahah-wrapper .form-item').hide();
		}
	});
}

function validateContactForm() {
    var contactemail1 = new LiveValidation('edit-submitted-e-mail-address', {
        validMessage: '.',
        wait: 500,
        onlyOnBlur: true
    });
    contactemail1.add(Validate.Presence, {
        failureMessage: "Email required"
    });
    contactemail1.add(Validate.Email, {
        failureMessage: "Enter a valid email"
    });
    var contactemail2 = new LiveValidation('edit-submitted-confirm-e-mail-address', {
        validMessage: '.',
        wait: 500,
        onlyOnBlur: true
    });
    contactemail2.add(Validate.Confirmation, {
        match: 'edit-submitted-e-mail-address',
        failureMessage: "Emails must match"
    });
}


