// Variables for AddThis Bookmark Button
addthis_url    = location.href;
addthis_title  = document.title;
addthis_pub    = 'indigo';

//Catalogue Brand Popup
function ViewProduct(psrc, pname) {
	var popUp = window.open(psrc, pname,"height=150, width=520");
	popUp.title = 'Brand Information';
	popUp.focus();
}

//Catalogue Colour Popup
function ViewProductB(psrc, pname) {
	var popUp = window.open(psrc, pname,"resizable=yes,width=400,height=500,scrollbars=yes");
	popUp.title = 'Colour Chart';
	popUp.focus();
}

//Custom Quote Calc
function checkCustomQuoteForm() {
	// This function is triggued when the form is changed
	var valid_form = true;

	// Fetch the values of the form
	var form = $('customquote_form');
	var qty = $F( form['data[CustomQuote][qty]'] );
	var locations = $F( form['data[CustomQuote][locations]'] );
	var quote_type = getValueRadio(form['data[CustomQuote][type]']);

	// Check if the values are valid
	if(quote_type == null || !quote_type.length) { valid_form = false;	}
	else {
		if(quote_type == 'printed') {
			$('emb_quote_fields').hide();
			$('print_quote_fields').show();
		}
		else if(quote_type == 'embroidered') {
			$('emb_quote_fields').show();
			$('print_quote_fields').hide();
		}

	}

	if(isNaN(qty) === true || qty <= 0) { valid_form = false; }
	if(!locations.length) { valid_form = false; }

	// Submit the form
	if(valid_form) {
		$('customquote_form').request({
			onSuccess: function(transport) {
//				alert(transport.responseText);
				$('perItemValue').innerHTML = transport.responseText;
			}
		});
	}




}

function getValueRadio(radioObj) {
	if(!radioObj) return null;

	var radioLength = radioObj.length;

	if(radioLength == undefined) {
		if(radioObj.checked) {	return radioObj.value; }
		else { return ""; }
	}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return null;
}

Event.observe(window, 'load', function() {
//		new Ajax.Autocompleter("search_field", "autocomplete_choices", "/products/autocomplete", {minChars: 3});

		// Replace telephone links with spans
		$$("a[href^=tel]").each(function(a) {
			var newSpan = document.createElement("span");
			newSpan.setAttribute("class", "tel");
			newSpan.appendChild(a.firstChild);
			a.up().replaceChild(newSpan, a);

		});
});

//Nav - FT style
/*global $ $$ Class*/
var FT = FT || {};

FT.Page = Class.create({
    initialize: function() {
        this.documentBody = $$("body")[0];

        // Menu show / hide - Initalise all the dropdown menus on the page
        $$(".menu").each(function(el){

            // cache the body click behaviour (so we can stop observing it temporarily later)
            var bodyClick = function(){
                setTimeout(function() {
                    el.removeClassName("menuopen");
                    this.documentBody.stopObserving("click", bodyClick);
                }.bind(this),1);
            }.bindAsEventListener(this);

            // cache the menu click behaviour (so we can stop observing it temporarily later)
            var menuClick = function(){
                this.documentBody.stopObserving("click", bodyClick);
                el.addClassName("menuopen");
                setTimeout(function(){
                    this.documentBody.observe("click", bodyClick);
                }.bind(this),1);
            }.bindAsEventListener(this);

            // observe the user clicking on the menu div (the arrow)
            el.observe("click", menuClick);

            // observe the user clicking the close button on the menu
            el.down("span").observe("click", function() {
                el.stopObserving("click", menuClick);                        // temporarily stop observing the menu div click
                el.removeClassName("menuopen");                             // undisplay the menu
                setTimeout(function(){ el.observe("click", menuClick); },1); // start observing the menu click again
            }.bindAsEventListener(this));

            // observe the user selecting an item from the menu
            el.select("li a").each(function(el) {
                el.observe( "click", this.menuChangeHandler.bindAsEventListener(this) );
            }.bind(this));

            el.setStyle({visibility:"visible"});
        }.bind(this));

        // Unbind the click listeners for the Editions dropdown menu - these are just normal clicky links 
        $$('.menu-leftaligner .menu li a').each( function(el) {
            el.stopObserving( "click" );
        });

        // navigation - show / hide secondary nav
        $$(".navigation-content > li:not(li[class=nosub])").each(function(el){
            // cache the body click behaviour (so we can stop observing it temporarily later)
            var bodyClick = function(){
                setTimeout(function() {
                    el.removeClassName("navopen");
                    this.documentBody.stopObserving("click", bodyClick);
                }.bind(this),1);
            }.bindAsEventListener(this);

            // cache the menu click behaviour (so we can stop observing it temporarily later)
            var navClick = function(event){
                if(el.className == "navopen"){
                    setTimeout(function() {
                        el.removeClassName("navopen");
                        this.documentBody.stopObserving("click", bodyClick);
                    }.bind(this),1);
                }
                this.documentBody.stopObserving("click", bodyClick);
                el.addClassName("navopen");

                setTimeout(function(){
                    this.documentBody.observe("click", bodyClick);
                }.bind(this),1);

                event.preventDefault();
            }.bindAsEventListener(this);

            // observe the user clicking on the a tag
            el.select("i").each(function(tag){
                var mover = function(){
                    //tag.stopObserving("mouseout",mout);
                    tag.addClassName('hover');
                }.bindAsEventListener(this);

                var mout = function(){
                    tag.removeClassName('hover');
                }.bindAsEventListener(this);

                tag.observe("click", navClick);
                tag.observe("mouseover",mover);
                tag.observe("mouseout",mout);
            }.bind(this));

            el.up("ul").removeClassName("inactive");
        }.bind(this));


    },

    /**
     *  Handles the behaviour of menus and fires a custom event from the menu so that other observers can pick up changes
     */
    menuChangeHandler: function(event) {
        var el = event.target; //the anchor tag

        el.up("ul").select("li").each(function(theLi) {
            theLi.removeClassName("selected");
        }.bind(this));

        el.up("li").addClassName("selected");

        // Fire our custom event handler so anything observing this menu will be notified of the change
        var menuEl = el.up(".menu");
        menuEl.fire("menu:changed", {selectedEl: el});

        // Update the selected text
        menuEl.down(".current").update(el.innerHTML);

        // hijack the href and stop it from working
        event.preventDefault();

        // close the menu after a short delay
        setTimeout(function(){menuEl.removeClassName("menuopen");},300);
    }

});

document.observe("dom:loaded", function() {
	document.page = new FT.Page();
});
