/* * * * * * * * * * * * * * * * *
 * Various JavaScript functions  *
 * afends.com                    *
 * * * * * * * * * * * * * * * * */

var isScrolledDown = false; // global variables are bad

// scroll effect on product category pages
Event.observe(window, 'load', function() {
	$$('a[href^=#]:not([href=#])').each(function(element) {
		element.observe('click', function(event) {
			new Effect.ScrollTo(this.hash.substr(1), {duration: '0.5', offset: -90});
			Event.stop(event);
		}.bindAsEventListener(element))
	});
	$$('#category-products li.item').each( function(e) {
		Event.observe(e, 'mouseover', function() {
			$('category-products').addClassName('inactive');
		});
		Event.observe(e, 'mouseout', function() {
			$('category-products').removeClassName('inactive');
		});
	});
	if (location.href.indexOf("#") != -1) {
		var values = (window.location.hash).split('&');
		loadProduct(values[0].substring(1), values[1], values[2], '');

	}
});

// scroll to specific div
function doScrollTo(toDiv) {
	new Effect.ScrollTo(toDiv, {duration: '0.5', offset: -90});
}

var current_url = null;
// load iframe into div
function loadProduct(url, returnId, urlKey, productName) {
	if (typeof(window.history.pushState) == 'function') {
		window.history.pushState('Object', 'Afends - ' + productName, '/viewproduct/' + urlKey);
	}
	else {
		window.location.hash = url + '&' + returnId + '&' + urlKey;
	}
	var container = document.getElementById('product_view_container');
	if (url != current_url) {
		current_url = url;
		container.innerHTML = '<div class="close_button" onclick="doScrollTo(\'' + returnId + '\');"></div><div id="product_view_iframe"><iframe src="' + url + '" name="product_view" width="745" height="815" scrolling="no" frameborder="0"></iframe></div>';
	}
	new Effect.Appear('product_loading');
	new Effect.Appear('product_container_border');
	if ($('prev') != null) {
		new Effect.Appear('prev');
	}
	if ($('next') != null) {
		new Effect.Appear('next');
	}
	doScrollTo('product_view_container');
}

/*function doInitialise() {
		var productAddToCartForm = new VarienForm('product_addtocart_form');
		productAddToCartForm.submit = function(button){
			if (this.validator.validate()) {
				this.form.submit();
				if (button && button != 'undefined') {
					button.disabled = true;
				}
			}
		}.bind(productAddToCartForm);
}

function loadProduct(url, returnId) {
	new Ajax.Updater('product_view_container', url, {method: 'post', });
}*/

// increases cart quantity and price when a new product is added
function alterCart(addQty, addPrice) {
	var theQty = document.getElementById('theQty');
	var theTotal = document.getElementById('theTotal');
	theQty.innerHTML = parseInt(theQty.innerHTML) + parseInt(addQty);
	theTotal.innerHTML = (parseFloat(theTotal.innerHTML) + (parseFloat(addPrice) * parseInt(addQty))).toFixed(2);
	document.getElementById('cart_content').innerHTML = '<div id="close_x" onclick="new Effect.toggle(\'cart_content\', \'blind\', { duration: 0.3 }); new Effect.Fade(\'dark_overlay\');"></div><iframe src="/recently-added" width="235" height="180" scrolling="auto" frameborder="0" style="margin-top:10px;border:0"></iframe><div id="continue_to_cart"><a href="/checkout/cart">Continue to Checkout</a></div><div id="or_continue_shopping" onclick="new Effect.toggle(\'cart_content\', \'blind\', { duration: 0.3 }); new Effect.Fade(\'dark_overlay\');">or Continue Shopping</div>';
	new Effect.ScrollTo('the_top', { duration: 0.5 });
	//new Effect.toggle('cart_content', 'blind', { delay: 0.5, duration: 0.3 });
	new Effect.Appear('dark_overlay');
	new Effect.BlindDown($('cart_content'));
}

function closeCart() {
	$('cart_content').hide();
}

// scrolls to store locator map
function toLocation(address) {
	document.getElementById('address').value = address;
	storeLocator.searchbystate($('address').value);
	new Effect.ScrollTo('storelocator-container', {duration: '0.5', offset: -90});
	return false;
}

// loads a new image (for single product view)
function loadProductImage(newImgSrc) {
	var base_image = document.getElementById('image');
	base_image.setAttribute('src', newImgSrc); // replace image
}

// toggle shop menu
function menuToggle(id) {
	new Effect.toggle(id, 'blind', { duration: .3 });
	return false;
}

function setPrev(prev_url, prev_sku) {
	prev = document.getElementById('prev_container');
	if (!prev_url) {
		prev.innerHTML = '';
	}
	else {
		prev.innerHTML = '<div id="prev" onclick="loadProduct(\'' + prev_url + '\', \'' + prev_sku + '\');"></div>';
	}
}

function setNext(next_url, next_sku) {
	next = document.getElementById('next_container');
	if (!next_url) {
		next.innerHTML = '';
	}
	else {
		next.innerHTML = '<div id="next" onclick="loadProduct(\'' + next_url + '\', \'' + next_sku + '\');"></div>';
	}
}

function showMoreImgsBg() {
	//$("more_imgs_bg").appear({ duration: 1.0 });
}

/*var selected_size = null;
function selectSize(e, size_id) {
	$('attribute121').value = size_id;
	if (selected_size != null) {
		selected_size.removeClassName('selected');
	}
	e.addClassName('selected');
	selected_size = e;
}*/

function showSizeChart() {
	new Effect.Appear('dark_overlay');
	new Effect.Appear('size_chart');
}

function hideSizeChart() {
	new Effect.Fade('size_chart');
	new Effect.BlindUp($('cart_content'));
	new Effect.Fade('dark_overlay');
}

jQuery(document).ready(function() {
	var height = jQuery(document).height();
	jQuery(document).scroll(function() {
		if (height > 1000) {
			if (jQuery(document).scrollTop() > 150) {
				//jQuery('#floating_header').addClass('floating', 500);
				isScrolledDown = true;
				jQuery('#nav_vert').css('position', 'fixed');
			}
			else {
				//jQuery('#floating_header').removeClass('floating', 500);
				isScrolledDown = false;
				jQuery('#nav_vert').css('position', 'static');
			}
		}
	});
});
