// loads tag highlighting if we need it

window.onload = function()
{
    el = document.getElementById('tag_input');
    if (el)
    {
        highlight_tags(el);
    }
}


// adds or removes a tag
function tag_swap(el, name)
{
    if (!find_tag(el, name))
    {
        var normalized = el.value.replace(/^\s*|\s*$/g,'');
        el.value = (normalized == '' ? '' : normalized + ' ') + name;
        highlight_tags(el);
    }
    else
    // tag already exists, remove it
    {
        tags = el.value.split(' ');
        new_tags = new Array();
        for (var i = 0; i <= tags.length; i++)
        {
            if (tags[i] != name)
            {
                new_tags[new_tags.length] = tags[i];
            }
        }
        el.value = new_tags.join(' ');
        clear_highlight();
        highlight_tags(el);
    }
}

// see if a tag is already in the block
function find_tag(el, name)
{
    tags = el.value.split(' ');
    for (var i = 0; i <= tags.length; i++)
    {
        if (tags[i] == name)
        {
            return true;   
        }
    }
    return false;
}

// highlights tags that are already there
function highlight_tags(el)
{
    tags = el.value.split(' ');
    clear_highlight();
    for (var i = 0; i <= tags.length; i++)
    {
        temp_tag = document.getElementById('tag_' + tags[i]);
        if (temp_tag)
        {
            temp_tag.setAttribute('class', 'tag_selected');
        }
    }
}

// clears all highlighted tags
function clear_highlight()
{
    highlighted_tags = getElementsByClass('tag_selected');
    for (var x = 0; x < highlighted_tags.length; x++)
    {
        highlighted_tags[x].setAttribute('class', 'tagging');
    }
}

// gets an array of all elements with `classname`
function getElementsByClass(classname)
{
    var elements = new Array();
    var inc = 0;
    var alltags = document.all ? document.all : document.getElementsByTagName('*');
    for (var z = 0; z < alltags.length; z++)
    {
        if (alltags[z].className == classname)
        {
            elements[inc++] = alltags[z];
        }
    }
    return elements;
}

function add_to_cart(id) {
	Element.show('indicator_'+id);
	var myAjax = new Ajax.Updater('cart-wrapper', '/product/add_to_cart/'+id, {
		method: 'get',
		onComplete: function() {
			Element.hide('indicator_'+id);
			show_cart();
		}
	});
}

function remove_from_cart(id) {
	Element.show('big_cart_indicator');
	var myAjax = new Ajax.Updater('cart-wrapper', '/product/delete_item/'+id, {
		onComplete: function() {
			Element.hide('big_cart_indicator');
		}
	});
}

function update_quantity(deliverable, quantity) {
	Element.show('big_cart_indicator');
	var params = "id=" + deliverable + "&quantity=" + quantity;
	var myAjax = new Ajax.Updater('cart-wrapper', '/product/update_quantity/'+deliverable, {
		method: 'post',
		parameters: params,
		onComplete: function() {
			Element.hide('big_cart_indicator');
		}
	});
}

function load_cart() {
	Element.show('indicator_cart');
	var myAjax = new Ajax.Updater('cart-wrapper', '/product/display_cart', {
		method: 'get',
		onComplete: function() {
			Element.hide('indicator_cart');
			show_cart();
		}
	});
}

function show_cart() {
	// set height of wrapper to height of content
	//var content_h = Element.getHeight('content-wrapper') + 40;
	//var window_h = getWindowHeight();
	//var cart_h = (content_h >= window_h) ? content_h : window_h;
	//$('cart-wrapper').style.height = cart_h + 'px';
	Effect.BlindDown('cart-wrapper', {
		duration: 0.8
	});
}

function hide_cart() {
	Effect.BlindUp('cart-wrapper', {
		duration: 0.4
	});
}

function getWindowHeight() {
	var h = 0;
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		h = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		h = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		h = document.body.clientHeight;
	}
	return h;
}

function load_image(img_src) {
	var im = new Image();
	im.src = img_src;
	new Effect.Fade('img_full', {
		duration: 0.5,
		afterFinish: function() {
			change_image(im);
		}
	});
}

function change_image(im) {
	$('img_full').src = im.src;
	new Effect.Appear('img_full', {duration: 0.5});
}

function same_as_billing() {
	var fields = new Array('first_name', 'last_name', 'address1', 'address2', 'city', 'state', 'zip', 'country');
	for (var i = 0; i < fields.length; i++) {
		$("order_ship_to_"+fields[i]).value = $("order_"+fields[i]).value;
	}
}

function init() {
	// warn IE 5 Mac users
	if (/MSIE/.test(navigator.userAgent) && /Mac/.test(navigator.userAgent)) {
		alert("This site does not support IE5 Mac. Please use the following supported Mac browsers: Safari 1.2+, Firefox 1.0+");
	}
}


function pop(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=300,height=400,scrollbars=yes');
return false;
}

function MM_jumpMenu(targ,selObj,restore){ 
			eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
			if (restore) selObj.selectedIndex=0; }


function popMov(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=440,height=440,scrollbars=yes');
return false;
}

function popMp3(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=220,height=240,scrollbars=yes');
return false;
}
