var input = {};
var slideshow = {};

/*
 * Converts the first letter of the given string into uppercase
 */
function strFirstUpper(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

/*
 * Sets the active menu item to the proper class. Along with the #nav-links a:active property in CSS, this helps to form "breadcrumbs"
 */
function activateMenuItem() {
	
	// Get url and split into variables
	var loc = new String(window.location).split('http://shop.611lifestyle.com/')[1];
	
	// only proceed with the function if this is a shop page
	if (loc != undefined) {
		loc = loc.split('/');
		
	
		// Set section to collection or product
		var section = loc[0];
	
		// Define activate class variable based on url
		switch(section) {
		case 'collections' :
	
			var collection = strFirstUpper(loc[1]);
			var type = (loc[2] != undefined) ? loc[2] : '';
			var activate = (type != '') ? collection + '-' + type : collection;
		
			break;
		case 'products' :
	
			var str = $('#product-info').attr('class');
			var collection = str.split(' ')[0];
			//
			switch (true) {
			case (str.indexOf('Music') > -1) :
				var activate =  'Music-' + collection;
				break;
			case (str.indexOf('Accessories') > -1) :
				var activate = 'Accessories-' + collection;
				break;
			case (str.indexOf('Womens') > -1) :
				var activate = 'Womens-' + collection;
				break;
			case (str.indexOf('Mens') > -1) :
				var activate = 'Mens-' + collection;
				break;
			}
			break;
		}
	
		$('.active').removeClass('active');
	
		if (type == '') {
			$('#menu li.' + activate).addClass('active');
		} else {
			$('#menu li[class^="' + activate +'"]').addClass('active');
		}
	}
}


/*
 * Functions for controlling the slideshow(s)
 */
function slidePause(n) {
	switch(slideshow.arr[n].active) {
	case true :
		clearInterval(slideshow.arr[n].process);
		slideshow.arr[n].active = false;
		$('[name="slideshow-' + n + '"] .slide-control').attr('src','images/slide-control-play.png');
		break;
	case false :
		slideFwd(n);
		slideshow.arr[n].process = setInterval(function() { slideFwd(n) },3000);
		slideshow.arr[n].active = true;
		$('[name="slideshow-' + n + '"] .slide-control').attr('src','images/slide-control-pause.png');
		break;
	}
}
function slideFwd(n) {
	if (slideshow.arr[n].cur == slideshow.arr[n].max) {
		slideshow.arr[n].nam = 'slide-' + n + '-1';
		$('.slide[name="' + slideshow.arr[n].nam + '"]').fadeIn('slow',function() {
			$('.slide:hidden').show();
			slideshow.arr[n].cur = 1;
		});
	} else {
		slideshow.arr[n].nam = 'slide-' + n + '-' + slideshow.arr[n].cur;
		$('.slide[name="' + slideshow.arr[n].nam + '"]').fadeOut('slow');
		slideshow.arr[n].cur++;
	}
}
function slideBack(n) {
	if (slideshow.arr[n].cur == 1) {
		$('.slide[name^="slide-' + n +'"]').not(':first').not(':last').hide();
		$('[name="slide-' + n + '-1"]').fadeOut('slow',function() {
			slideshow.arr[n].cur = slideshow.arr[n].max;
		});
	} else {
		slideshow.arr[n].cur--;
		$('[name="slide-' + n + '-' + slideshow.arr[n].cur + '"]').fadeIn('slow');
	}
}

/*
 * Aligns #content with #menu, and gives precedence to the taller element.
 */
function alignMain() {
	if ($('#menu').length > 0 && $('#content').length > 0) {
		var m = $('#menu').height();
		var c = $('#content').height();
		if ((m+30)>c) {
			$('#content').css('height' , (m+30)+'px');
		} else if (c>(m+30)) {
			$('#menu').css('height' ,(c-30)+'px');
		} else {
			$('#content,#menu').css('height' , 'auto');
			alignMain();
		}
	}
}
//
// DOM Ready
$(document).ready(function() {
	alignMain();
	activateMenuItem();
	
	//
	// Twitter handler. Only called if there is a #twitter feed on the page
	if ($('#tweets').length > 0) {
		// make an API call to twitter.com and find the last 3 tweets
		$("#tweets").tweet({
			username: "nigel611",
			avatar_size: 0,
			count: 3,
			loading_text: "<img src=\"images/spinner.gif\" alt=\"loading tweets...\" style=\"margin-top: -20px\" />",
			callback: function() {
				alignMain();
			}
		});
	}	
	
	//
	// Default text handler
	input.replace = false;
	$('input[type="text"]').focus(function() {
		input.val = $(this).attr('value');
		if ($(this).attr('value') != '') {
			input.replace = true;
			$(this).attr('value','');
		} else {
			input.replace = false;
		}
	});
	$('input[type="text"]').blur(function() {
		if (input.replace) {
			var val = $(this).attr('value');
			if (val == '') $(this).attr('value', input.val);
		}
	});
	
	//
	// Shop menu handler
	$('#menu li').click(function() {
		$('#menu .active').removeClass('active');
		$(this).addClass('active');
	});
	
	//
	// Slideshow handler
	slideshow.n = 0;
	slideshow.arr = new Array();
	$('.slideshow').each(function() {
		$(this).attr('name','slideshow-' + slideshow.n);
		slideshow.slideCount = $('.slide',this).length;
		if (slideshow.slideCount > 0) {
			var n = 0;
			$('.slide',this).hide();
			$('.slide',this).each(function() {
				var z = 99 - n;
				n++;
				slideshow.num = $(this).parents('.slideshow').attr('name').split('-')[1];
				$(this).css({ 'z-index':z , 'position':'absolute'}).attr('name','slide-' + slideshow.num + '-' + n);
				if (n == slideshow.slideCount) {
					$('.slide').show();
					slideshow.arr[slideshow.num] = {};
					slideshow.arr[slideshow.num].active = true;
					slideshow.arr[slideshow.num].process = setInterval(function() { slideFwd(slideshow.num) },3000);
					slideshow.arr[slideshow.num].cur = 1;
					slideshow.arr[slideshow.num].max = slideshow.slideCount;
				}
			});
			
			//
			// Create slideshow controller if multiple slides
			if (slideshow.slideCount > 1) {
				
				var controlStr = '<img src="images/slide-control-pause.png" class="slide-control">'
				+'<div class="slide-back"></div>'
				+'<div class="slide-pause"></div>'
				+'<div class="slide-forward"></div>';
				//
				$(this).append(controlStr);
				//
				$('.slide-back').click(function() {
					var show = $(this).parents('.slideshow').attr('name').split('-')[1];
					if(slideshow.arr[show].active) slidePause(show);
					slideBack(show);
				});
				$('.slide-pause').click(function() {
					var show = $(this).parents('.slideshow').attr('name').split('-')[1];
					slidePause(show);
				});
				$('.slide-forward').click(function() {
					var show = $(this).parents('.slideshow').attr('name').split('-')[1];
					if(slideshow.arr[show].active) slidePause(show);
					slideFwd(show);
				});
			}
		}
		slideshow.n++;
		
		//
		// Cart handler plugins
		/*
		 * This plugin removes the selected row from the Cart, and then re-submits the Cart form, effectively deleting the LineItem.
		 * Assumes that it will only be called on /cart, and therefore a #cart element will be available.
		 */
		$.fn.removeFromCart = function() {
			var variant = $(this).attr('id').substr(4);		// find variant.id from #id
			$('#updates_'+variant).val(0);								// set quantity for this item to 0
			$(this).hide();																// remove from view
			$('#cart').submit();													// recompile line_items (which should remove this item from cart)
		};
	});
});
