
// nav setting
var locked = false;

// generic audio settings

var flashvars = {};
flashvars.settingsXML = www_path + "xml/settings.xml";
flashvars.swfwidth = 224;
			
var params = {};
params.menu = "false";
params.allowscriptaccess = "always";
params.swliveconnect = "true";
params.bgcolor = "FFFFFF";

var featured_item = 0;
var featured_btns = false;
var total_featured_items = false;
var featured_active = true;

var top_item = 10000;
var attributes = {};
var clearedFields = new Array();
var disable_scrolling = false;
var subscribe_attempts = 0;

function hideAllNav() {
	if (!locked) {
		$('ul.subnav').css('display', 'none');
		$('ul.subnav').css('height', '0px');
	}
}

function resizeSWF(nHeight, id) {
	nHeight += "px";
	document.getElementById(id).style.height = nHeight;			
}

$(document).ready(function () {
	
	$('div.frame-featured, div.frame-side-ad').mouseover(function() {
		$('a', $(this)).css('opacity', '.35');
		$('span.title', $(this)).css('display', 'block');
	});
	
	$('div.frame-featured, div.frame-side-ad').mouseout(function() {
		$('a', $(this)).css('opacity', '1');
		$('span.title', $(this)).css('display', 'none');
	});
	
	// side nav rollovers
	if ($('ul#side-nav').length > 0 && $('div#side-nav-cloud').length > 0) {
		$('ul#side-nav a').mouseover(function(e) {
			var title = $(this).attr('rel');
			if (title != '') {
				var pos = $(this).position();
				$('div#side-nav-cloud').html(title);
				$('div#side-nav-cloud').css('left', e.pageX + 'px');
				$('div#side-nav-cloud').css('top', (pos.top + 20) + 'px');
				$('div#side-nav-cloud').css('display', 'block');
			}
		});
		$('ul#side-nav a').mouseout(function() { $('div#side-nav-cloud').css('display', 'none'); });
	}
	
	// top login box rollover
	if ($('div#topbox a').length > 0) {
		$('div#topbox a').mouseover(function(e) {
			var pos = $('div#topbox').position();
			$('div#topbox-cloud').css('left', e.pageX + 'px');
			$('div#topbox-cloud').css('top', (pos.top - 52) + 'px');
			$('div#topbox-cloud').css('display', 'block');
		});
		$('div#topbox-cloud').mouseover(function(e) {
			$('div#topbox-cloud').css('display', 'block');
		});
		$('div#topbox a, div#topbox-cloud').mouseout(function() { $('div#topbox-cloud').css('display', 'none'); });
	}
	
	// move with the mouse
	$().mousemove( function(e) {
		var dis = $('div#side-nav-cloud').css('display');
		if (dis == 'block') {
			$('div#side-nav-cloud').css('top', (e.pageY + 20) + 'px');
			$('div#side-nav-cloud').css('left', e.pageX);
		}
	});
	
	// disable scrolling when typing in input fields
	$('input, textarea').focus(function() { disable_scrolling = true; });
	$('input, textarea').blur(function() { disable_scrolling = false; });
	
	// scroll machine
	// key triggers
	
	var selector = 'div.column-main div.item';
	var items = $(selector);
	
	if (items.length > 0) {
		var current_item = 0;
		var top_start = $(items[0]).position().top;
		var top_offset = 10;
		// find the current item - if they refreshed and the scoll position wasn't at the top of the page
		var scroll_position = $(window).scrollTop();
		if (scroll_position > 0) {
			var found = false;
			$(items).each(function(item) {
				var t = $(this).position().top;
				if (!found && t > scroll_position) {
					current_item = $(selector).index(this);
					found = true;
				}
			});
		}
		if (current_item == 0) { current_item = top_item; }
		$(this).bind('keypress', function(e) {
			if (!disable_scrolling) {
				var key = (e.keyCode ? e.keyCode : e.charCode);
				if (key == 107 || key == 109) {
					// m was pressed - go down
					if (key == 109) {
						if (current_item == top_item) {
							current_item = 0;
						}
						else {
							current_item++;
							if (current_item == items.length) {
								current_item = items.length-1
							}
						}
					}
					// k was pressed - go up
					else if (key == 107) {
						if (current_item != top_item) {
							current_item--;
							if (current_item < 0) {
								current_item = top_item;
							}
						}
					}
					// scroll to the item
					if (items[current_item] || current_item == top_item) {
						if (current_item == 0) { var top = top_start - top_offset; }
						else if (current_item == top_item) {
							var top = 0;
						}
						else {
							var position = $(items[current_item]).position();
							//alert(parseInt(Math.round(position.top)));
							var top = parseInt(Math.round(position.top)) - top_offset;
						}
						top = top - top_offset;
						$('html,body').animate({scrollTop: top}, 300);
					}
				}
			}
		});
	}
	
	// set up the audio players
	$('div.audioplayer a').each(function() {
		var href = $(this).attr('href');
		var p = $(this).parent().get(0);
		var id = $(p).attr('id');
		if (id) {
			var href = $(this).attr('href');
			href = escape(href);
			attributes.id = id;
			flashvars.id = id;
			flashvars.dataXML = href;
			swfobject.embedSWF(www_path + "flash/player.swf", id, "224px", "33px", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
		}
	});
	
	
	// sliding featured banner
	if ($('div.featured').length > 0) {
		featured_btns = $('div.featured ul.buttons a');
		total_featured_items = $('div.featured div a').length;
		featuredSlider();
		$(featured_btns).click(function(e) {
			e.preventDefault();
			featured_active = false;
			var index = $(featured_btns).index(this);
			featuredSlideTo(index);
		});
	}

	// shop stuff
	$('input#billing').click(function() {
		var checked = $(this).attr('checked');
		$('form#addresses input, form#addresses select').each(function() {
			var prefix = $(this).attr('name').split('_');
			if (prefix[0] == 'shipping' && prefix.length > 1) {
				var val = checked ? $(this).attr('value') : '';
				$("[name='billing_" + prefix[1] + "']").attr('value', val);
			}
		});
	});
	
	// clear the fields
	$('input.clearfield').click(function() {
		var index = $('input.clearfield').index($(this));
		if (jQuery.inArray(index, clearedFields) == -1) {
			$(this).attr('value', '');
			clearedFields[index] = index;
		}
	});
	
	// needs to be set in a seperate method because it needs to be re-established once
	subscribeForm();
	
	// button replacements for forms
	$('form').each(function() {
		var btn = $('a.form-submit', $(this));
		if (btn.length > 0) {
			var f = $(this);
			$('[type=submit]', $(this)).css('display', 'none');
			$('a.form-submit', $(this)).css('display', 'inline');
			$('a.form-submit', $(this)).click(function(e) {
				e.preventDefault();
				$(f).submit();
			});
		}
	});
	
	// country changer
	// if set to Australia - we show the state dropdown
	if ($('#country_dropdown').length > 0 && $('#state_dropdown').length > 0) {
		$('#country_dropdown').change(function() {
			$('#state_dropdown').css('display', $(this).attr('value')==1 ? 'block' : 'none');
		});
	}
	
	$('input.password').attr('value', '');
	
});

function featuredSlider() {
	if (featured_active) {
		if (featured_item == total_featured_items) {
			featured_item = 0;
		}
		featuredSlideTo(featured_item);
		featured_item++;
		setTimeout('featuredSlider()', 5000);
	}
}

function featuredSlideTo(item) {
	var d = $('div.featured div');
	var w = $('div.featured').width();
	var left = item*-w;
	$(d).animate({left: left}, 500);
	$(featured_btns).removeClass('selected');
	$(featured_btns[item]).addClass('selected');
}

function subscribeForm() {
	
	// subscribe module
	$('div.subscribe a').click(function(e) {
		e.preventDefault();
		var cl = $(this).attr('class');
		
		// close - cookie stuff
		if (cl == 'subscribe-close') {
			hideSubscribeForm();
		}
		// submit - already has global actions to post the form
		else if (cl == 'form-submit') {
			$(this).unbind('click'); // remove all current actions
			var f = $(this).parents('form');
			var url = $(f).attr('action');
			var post_data = $(f).serialize() + '&ajax=1';
			var form_html = $('div.subscribe-body form').html();
			$('div.subscribe-body form').html('Processing...');
			$.post(url, post_data,
				function(data){
					if (data.code == 208) { $('div.subscribe-body').animate({height:'140px'}, 300); }
					if (data.code == 0) { $('div.subscribe-body').animate({height:'115px'}, 300); }
					if (data.error == 1) {
						$('div.subscribe-body').animate({height:'100px'}, 300);
						var error_field = $('div.subscribe-body form span.error');
						if (subscribe_attempts > 1) {
							$('div.subscribe-body form').html(form_html);
							$('div.subscribe-body form span.error').html(data.message);
						}
						else {
							$('div.subscribe-body form').html('<span class="error">' + data.message + '</span><br />' + form_html);
						}
						subscribeForm();
					}
					else {
						$('div.subscribe-body form').html('<div class="message">' + data.message + '</div>');
						setTimeout(function() { hideSubscribeForm(); }, 8000);
					}
			  	}, 
				"json"
			);
			subscribe_attempts++;
		}
		
		// open the form up
		else {
			$('div.subscribe-body').animate({height:'90px'}, 300);
			$(this).animate({opacity:0}, 500,  
				function() {
					$(this).remove();
					$('div.subscribe form').css('opacity', 0);
					$('div.subscribe form').css('display', 'block');
					$('div.subscribe form').fadeTo(300, 1);
				}
			);
		}
	});
	
}

function hideSubscribeForm() {
	$('div.subscribe').fadeTo(300, 0,  
		function() { 
			$('div.subscribe').slideUp(500, function() { $('div.subscribe').remove(); }) 
		}
	);
	$.ajax({type: "POST",url: www_path + 'subscribe/remove/'});
}