// jQuery.noConflict();

// add to head.phtml
/*
<script type="text/javascript">
var BASE_URL = '<?php echo $this->getBaseUrl() ?>';
var MODULE = '<?php echo Mage::app()->getRequest()->getModuleName() ?>';
var CUR_URL = '<?php echo Mage::helper('core/url')->getCurrentUrl() ?>';
var DEV_MODE = '<?php echo isset($_SERVER['MAGE_IS_DEVELOPER_MODE']) ? 1: 0; ?>';
</script>
*/

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

DEV_MODE = true

function cl() {
    if (DEV_MODE && typeof(console) != 'undefined') {
        var delimiter = ' : ';
        var msg = '';
        var args = $A(arguments);
        var argsLength = args.length;
        for(i=0;i<argsLength;i++){
            if(i==argsLength-1) delimiter = '';
            msg += args[i] + delimiter;
        }
        console.log(msg);
    }
}

var testEnumerator = 0;
function cltest(){
    if(DEV_MODE) {
        testEnumerator++;
        var args = $A(arguments);
        var testArgs = (args.length < 1) ? 'test: ' + testEnumerator : args;
        if(Prototype.Browser.Gecko) {
            cl(testArgs);
        } else {
            alert(testArgs);
        }
    }
}

function toggleMenu(el, over, second) {
    if (!second && !over) {
        setTimeout(function(){toggleMenu(el, 0, 1);}, 100);
        el.setAttribute('sameAttribute', 1);
        return;
    } else if (!over && el.getAttribute('sameAttribute') == 0)
    return;
    el.setAttribute('sameAttribute', 0);

    var addClass = Element.addClassName;
    var removeClass = Element.removeClassName;
    
    if (over) {
        addClass(el, 'over');
    } else {
        addClass(el, 'hold');
        removeClass(el, 'over');
        setTimeout(function(){removeClass(el, 'hold');}, 300);
    }
}


document.observe('dom:loaded', function() {
    $$('a[rel="external"]').each(function(link) {
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
            link.writeAttribute('target', '_blank');
        }
    });
    
    // David Robinson - logic to add checkbox under all customer_company fields on site,
    // and event observer to remove/add required.
	// EDIT: Chris Nanninga <chris.nanninga@classyllama.com> - Modifications so this also works on checkout page //
	var companyId = 'customer_company';
	var notCompanyId = 'not_company';
	var notCompanyName = 'not_company';
	var firstnameId = 'firstname';
	var lastnameId = 'lastname';
	var company = $(companyId);
	if (!company) {
		companyId = 'billing:' + companyId;
		notCompanyId = 'billing:' + notCompanyId;
		notCompanyName = 'billing[' + notCompanyName + ']';
		firstnameId = 'billing:' + firstnameId;
		lastnameId = 'billing:' + lastnameId;
		company = $(companyId);
	}
    if (company) {
        toggleRequiredEnabled(company, 'Company');
        boxli = $(document.createElement('li'))
        boxli.update(
        '<label for="' + notCompanyId + '">I\'m not a Company</label>' +
        '<div class="input-box">' +
            '<input type="checkbox" name="' + notCompanyName + '" title="I\'m not a Company" value="1" id="' + notCompanyId + '" class="checkbox" autocomplete="off">' +
        '</div>'
        );
        company.up(1).insert({
            'after' : boxli
        });
		var notCompany = $(notCompanyId);

        notCompany.observe('click', function() {
            toggleRequiredEnabled(company, 'Company');
			if (notCompany.checked && (adviceBox = $('advice-required-entry-' + companyId))) {
				adviceBox.hide();
			}
        });
        
        firstname = $(firstnameId);
        lastname = $(lastnameId);
        
        if (firstname && lastname && company.value == firstname.value + ' ' + lastname.value) {
            notCompany.click();
        }
    }
    //-- removes delay from main nav
    mainNav("nav", {"show_delay":"0","hide_delay":"0"});
    
    $$('a[rel="external"]').each(function(link) {
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
            link.writeAttribute('target', '_blank');
        }
    });
});

// Toggles field betwee required/enabled and not required/disabled, as well as updates label
function toggleRequiredEnabled(el, label) {
    if (el.hasClassName('required-entry')) {
        el.removeClassName('required-entry')
            .disable();
        Validation.reset(el);
        Validation.hideAdvice(el, $('advice-required-entry-customer_company'));
        el.value = '';
        el.up().previous()
            .update(label)
            .removeClassName('required');
    } else {
        el.addClassName('required-entry')
            .enable();
        el.up().previous()
            .update('<em>*</em>' + label)
            .addClassName('required');
        }
}

