// JavaScript Document

var settings = {
	messages	:	'messages',
	messages_hide_delay	: 0.5
};

function init(e) {
	//check if the messages element exists and is visibile,
	//if so, apply the highlight effect to it
	var messages = $(settings.messages);
	
	if (messages && messages.visible()) {
		new Effect.Highlight(messages);
	}
	
	//sidebar loaders
		
	
}

Event.observe(window, 'load', init);

function message_write(message) {
	var messages = $(settings.messages);
	if (!messages)
		return;
	
	if (message.length == 0) {
		messages.hide();
		return;
	}
	
	messages.update(message);
	messages.show();
	new Effect.Highlight(messages);
}

function message_clear() {
	setTimeout("message_write('')", settings.messages_hide_delay * 1000);
}

function loadSlider(id) {
	var toOpen = $(id+'-box');
	var currentOpen = $('side-nav-list').down('div.side-item-visible');
	
	if (toOpen == currentOpen)
		return;
		
	if (currentOpen) {
		currentOpen.removeClassName('side-item-visible');
		new Effect.BlindUp(currentOpen);
		
		var currentLi = $(currentOpen.id.sub('-box', ''));
		currentLi.removeClassName('side-nav-selected');
	}
	
	if (toOpen) {
		currentLi = $(toOpen.id.sub('-box', ''));
		currentLi.addClassName('side-nav-selected');
		
		toOpen.addClassName('side-item-visible');
		new Effect.BlindDown(toOpen, {queue : 'end'});	
	}
	
}
function fadeDiv(id) {
	$(id).fade();
}
function productSubscribeBox(pid) {
	var boxExists = $('product-subscribe-container');
	if (boxExists && boxExists.visible())
		return;
		
	if (boxExists) {
		boxExists.appear();
		return;
	}
	var attrs = {
		id : 'product-subscribe-container',
		style : 'display:none'
	}
	var box = new Element('div', attrs);	
	var html = '<div id="product-subscribe-box"><form method="post" action="/cart/productsubscribe" id="product-subscribe-form"><input type="hidden" name="product_id" value="'+pid+'"/><div class="row"><label for="form_name">Your Name:</label><input type="text" name="name" id="form_name"  /></div><div class="row"><label for="form_email">Your Email:</label><input type="text" name="email" id="form_email"  /></div><div class="row"><img src="/utility/captcha" alt="CAPTCHA image" /><div><label for="form_captcha">Enter Above Phrase:</label><input type="text" name="captcha" id="form_captcha"/></div></div><div><a href="#" onclick="contactSubmit(\'product-subscribe-form\', \'product-subscribe-box\'); return false;"><img src="/resources/images/submit.gif" alt="Submit" title="Submit" /></a></div></form></div><div id="product-subscribe-footer"><a href="#" onClick="fadeDiv(\'product-subscribe-container\'); return false;">Close Window</a></div>';
	
	box.update(html);
	
	
	$('left-content').insert(box);
	box.appear();
}

function loadActivity(e) {
	if (e.findElement('#sidebar-contact-form'))
		return;
	Event.stop(e);
	//li that was clicked
	var clickedItem = e.findElement('li');
	//first div in that li
	var divItemId = clickedItem.id+'-box';
	var divItem = $(divItemId);
	//get the currently opened item, if any
	var openDiv = $('side-nav-list').down('div.side-item-visible');
	
	//if user clicked on already open item do nothing
	if (openDiv == divItem)
		return;
	
	
	//remove the class name tag and hide the div
	if (openDiv) {
		//change class of prev opened div and hide
		openDiv.removeClassName('side-item-visible');
		new Effect.BlindUp(openDiv);
		
		//change class of prev selected li
		var prevLi = $(openDiv.id.sub('-box',''));
		prevLi.removeClassName('side-nav-selected');
		
		
	}	
	
	if (divItem) {
		//change class of selected li so it stands out
		clickedItem.addClassName('side-nav-selected');
		
		//change the css class and show the div
		divItem.addClassName('side-item-visible');
		new Effect.BlindDown(divItem, {queue : 'end'});
	}
}


function contactSubmit(formId, div) {
	var form = $(formId);
	var container = $(div);
	container.innerHTML = '<div class="loading"><img src="/resources/images/loading4.gif"/> Sending Request...</div>';
	form.request({
				onComplete: function(transport){
					container.update(transport.responseText);
				}
			})	
}

function confirmLink(href, message) {
	var promptMsg = 'Click OK to proceed';
	if (message) promptMsg = message;
	
	if (confirm(message))
		window.location = href;
}
/*
ContactForm = Class.create();

ContactForm.prototype = {

    form   : null,
	container	: null,

    initialize : function(form, container)
    {
		this.container = $(container);
        this.form = $(form); this.form.observe('submit', this.onSubmit.bindAsEventListener(this));

    },

    onSubmit : function(e)
    {
        Event.stop(e);
		this.container.innerHTML = '<div class="loading"><img src="/resources/images/loading4.gif"/> Sending Request...</div>';

        var options = {
            parameters : this.form.serialize(),
            method     : this.form.method,
            onSuccess  : this.onFormSuccess.bind(this)
        };

        new Ajax.Request(this.form.action, options);
    },

    onFormSuccess : function(transport)
    {
        var response = transport.responseText;
		this.container.innerHTML = response;
    }
};*/
