/* FIVE FOUR CLOTHING */
/* Store Functions
----------------------------------------------------------------------------- */

	// AJAX request holder
	var request = null;

	// Check for hash
	$(function() {

		if ((page == 'index' || page == 'store' || page == 'browse') && document.location.hash) {

			if (document.location.hash == 'undefined') {
				var url = '';
			} else {
				var url = document.location.hash.replace(/#/, '?');
			}
		
			loadPage(document.location.pathname + url);

		}
	
	});

	$(document).ready(function() {

		$('#footer div.widget-alert').insertAfter('#header-banner');
		$('#footer div.widget-alert').insertAfter('#contest');

	});

	$(window).load(function() {

		markup();
		contest();
		mailinglists();

		if (page == 'item') {
			
			item();
			clothesHorse();
			
		}

		if (section == 'store') {
		
			sidebar();
			items();
			tweets();

			$('#content ul.pages a, #content ul.paging a').click(function() {
			
				loadPage($(this));
				
				return false;
			
			});
		
		}

		$('div.receipt').show();
		$('#main').css('visibility','visible');

	});

/* FUNCTION: MARKUP CHANGES
--------------------------------------------------------------------------- */

	function markup() {

		if (page == 'gift') {
		
			$('div.widget-item.gift div.purchase').prepend('<h3>Purchase A Gift Card</h3>');
		
		}
		
		if (section == 'store') {
		
			$('strong.all, a.all').text('View All');
			
			// Remove multiple highlight of sidebar tags
			$('body.kids_collection #mens strong').each(function() {
			
				$(this).children('a').insertAfter($(this));
				$(this).remove();
			
			});
			$('body.mens_collection #kids strong').each(function() {
			
				$(this).children('a').insertAfter($(this));
				$(this).remove();
			
			});
		
		}
	
	}


/* FUNCTION: CONTEST COOKIE
----------------------------------------------------------------------------- */

	function contest() {

		$("#contest div.form input.email").val("E-mail Address");

		$("#contest div.form input.submit").click(function() {

			if ($("#contest input.email").val() == "E-mail Address") {

				$("#contest input.email").val("");

			}

		});

		$("#contest div.form label.error").each(function() {

			var message = "E-mail Address Is Required";
			$(this).children('input[type=text]').val(message).attr("title", message);

		});

		$("#contest div.form input[type=text]").click(function() {

			var label = $(this).attr("title");
			if (label == $(this).val()) {

				$(this).val("");

			}

		});

		$("#contest div.form input[type=text]").blur(function() {

			var label = $(this).attr("title");
			if ($(this).val() == "") {

				$(this).val(label);

			}

		});

		$('#fivefour-contest').submit(function() {

			$.cookie('contest', 'entered', {expires: 1, domain: 'fivefourclothing.com', path: '/'});

		});

		var contest_submission = $.cookie('contest');
		if (contest_submission == "entered") {

			$('#contest div.form').hide();
			$('#contest div.message').show();

		}

	}


/* FUNCTION: MAILING LISTS
----------------------------------------------------------------------------- */
	
	function mailinglists() {

		$("#mailinglist #mailing_list_entry_email").val("Your E-mail Address").attr("title", "Your E-mail Address");
	
		$("#mailinglist input.save").click(function() {
	
			if ($("#mailinglist #mailing_list_entry_email").val() == "Your E-mail Address") {
	
				$("#mailinglist #mailing_list_entry_email").val("");
	
			}
	
		});
	
		$("#mailinglist label.error").each(function() {
	
			var message = "Your E-mail Address Is Required";
			$(this).children('input[type=text]').val(message).attr("title", message);
	
		});
	
		$("#mailinglist input[type=text]").click(function() {
	
			var label = $(this).attr("title");
			if (label == $(this).val()) {
	
				$(this).val("");
	
			}
	
		});
	
		$("#mailinglist input[type=text]").blur(function() {
	
			var label = $(this).attr("title");
			if ($(this).val() == "") {
	
				$(this).val(label);
	
			}
	
		});

	}


/* FUNCTION: ITEMS
--------------------------------------------------------------------------- */

	function items() {

		$('ul.items li').hover(function() {
	
			$(this).addClass('hover');
	
		}, function() {
	
			$(this).removeClass('hover');
	
		});
	
	}


/* FUNCTION: ITEM PAGE
--------------------------------------------------------------------------- */

	function item() {

		// Special labels (sale, etc.)
		$('div.topic ul.tags li').each(function() {

			var tags = $(this).attr('class');

			if (tags.match('label')) {

				var tag = $(this).text();
				var tag = tag.replace('Label - ','');
				$(this).text(tag);
				$(this).removeClass().addClass('label');

			}

		});

		// Load patterns
		$('div.widget-alert.added').cliqueAlertWidget();
		$('div.widget-item.display:not(.photos, .kids_collection)').cliqueItemWidget({

			checkStock: false,
			pressure: false,
			inlineSizes: false,

			preview: true,
			removeEmptyThumbs: false,
			zoom: 'drag',
			loupePosition: 'right',
			loupeWidth: '205',
			loupeHeight: '476',
			loupeOffsetY: '0'

		});
		$('div.widget-item.display.kids_collection').cliqueItemWidget({

			checkStock: false,
			pressure: false,
			inlineSizes: false,

			preview: false,
			removeEmptyThumbs: false,
			zoom: 'loupe',
			loupePosition: 'right',
			loupeWidth: '205',
			loupeHeight: '476',
			loupeOffsetY: '0'

		});

		// Photo drawer
		/* $('div.widget-item.photos').hoverIntent(function() {

			$('div.widget-item.photos ul').animate( { left: "-198px" }, "normal");

		}, function() {

			$('div.widget-item.photos ul').animate( { left: "0px" }, "normal");

		}); */

		// Dim sidebar browser
		/* $('#sidebar').addClass('dimmer');
		$('#sidebar').hover(function() {

			$(this).removeClass('dimmer');

		}, function() {

			$(this).addClass('dimmer');

		}); */

	}

/* FUNCTION: STORE SIDEBAR / MAKE GROUP COLLAPSIBLE
----------------------------------------------------------------------------- */

	function collapse(heading) {

		$(heading).click(function() {

			if ($(this).parent().hasClass('expanded')) {

				$(this).parent().removeClass('expanded');			
			
			} else {
	
				$(this).parent().addClass('expanded');
			
			}
			
			if ($(this).parents('div.collapsible').find('.expanded').length > 0) {
	
				$(this).parents('div.collapsible').removeClass('closed');
				$(this).parents('div.collapsible').addClass('opened');
			
			} else {
	
				$(this).parents('div.collapsible').removeClass('opened');
				$(this).parents('div.collapsible').addClass('closed');
	
			}
			
			return false;
		
		});
	
	}

/* FUNCTION: STORE SIDEBAR / LOAD PAGE
----------------------------------------------------------------------------- */

	function loadPage(button) {
	
		// Find out event type
		if ($(button).parents('ul').is('.pages') || $(button).parents('ul').is('.paging')) {
			var type = 'pagination';
			var link = $(button).attr('href');
		} else if ($(button).parents('ul').is('.toggle')) {
			var type = 'filter';
			var link = $(button).attr('href');
			try { request.abort(); } catch(e) {} // terminate other AJAX requests
		} else if ($(button).parents('div').is('.group')) {
			var type = 'collections';
			var link = $(button).attr('href');
		} else {
			var type = 'redirect';
			var link = button;
		}

		// Add overlay
		if ($.browser.msie) {
			$('#content').append('<div class="overlay"><div class="spinner"><img src="' + asset_host + '/images/layout/loader.gif" /></div>');
		} else {
			$('#content').append('<div class="overlay"><div class="spinner"><div class="bar1"></div><div class="bar2"></div><div class="bar3"></div><div class="bar4"></div><div class="bar5"></div><div class="bar6"></div><div class="bar7"></div><div class="bar8"></div><div class="bar9"></div><div class="bar10"></div><div class="bar11"></div><div class="bar12"></div></div></div>');		
		}
		if (type != 'pagination') $('#sidebar').append('<div class="overlay"></div>');
		if (type == 'redirect') $('#content div.overlay').css('background-color', '#fff');
		$('div.overlay').each(function() {		
			$(this).height($(this).parent('div').height()).fadeIn(500);
		});

		request = $.ajax({
	
			url:		link,
			success:	function(data) {

				// Trigger Google analytics page view tracking
				try { _gaq.push(['_trackPageview', link]); } catch(e) {}
		
				$('#content').replaceWith($(data).find('#content'));
				items();

				if (type == 'collections' || type == 'redirect') {

					$('#sidebar #collections').replaceWith($(data).find('#sidebar #collections'));
					$('#sidebar #filter').replaceWith($(data).find('#sidebar #filter'));

					// Refresh initializations
					collections();
					filter();

				}

				if (type == 'filter') {

					$('#sidebar #filter').replaceWith($(data).find('#sidebar #filter'));

					// Refresh initializations
					filter();

				}
				
				// Track event in Google analytics
				try { _gaq.push(['_trackEvent', page[0].toUpperCase() + page.slice(1) + ' Page', type[0].toUpperCase() + type.slice(1), link]); } catch(e) {}

				// Track goal in Google analytics
				// _gaq.push(['_trackPageview', link + '-ajax.html']);
				
				// Change URL hash
				if (type != 'redirect' && link.match(/\?/)) {

					var hash = link.split('?')[1];
					window.location.hash = '#' + hash;
				
				}
				
				// Pagination
				$('#content ul.pages a, #content ul.paging a').click(function() {
				
					loadPage($(this));
					
					return false;
				
				});
				
				// Scroll to top
				$("html:not(:animated), body:not(:animated)").animate({ scrollTop: $('#content').offset().top - 20}, 500 );
				
				// Kill overlay
				$('div.overlay').fadeOut(700, function() { $(this).remove(); });
	
			}
	
		});

		// Terminate AJAX request on ESC key press
		$(document).keyup(function(e) { 

			if (e.keyCode == 27) {
			
				// Kill overlay
				$('div.overlay').fadeOut(700, function() { $(this).remove(); });

				request.abort();
			
			}

		});
	
	}


/* FUNCTION: STORE SIDEBAR / INITIALIZE COLLECTIONS
----------------------------------------------------------------------------- */

	function collections() {

		// Add classes
		// $('#sidebar #collections strong').parent().addClass('selected').parents('div.list').addClass('selected');
		$('#sidebar #collections.opened > div').addClass('expanded');
		collapse('#sidebar #collections h3');
		$('#sidebar ul.special li, #sidebar div.widget-tag ul li').hover(
	
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
	
		);
		
		// AJAX collection links
		/* $('#collections a').click(function() {
		
			loadPage($(this));
			
			return false;
		
		}); */
	
	}


/* FUNCTION: STORE SIDEBAR / INITIALIZE FILTER
----------------------------------------------------------------------------- */

	function filter() {

		// Markup changes
		$('#sidebar #filter div.scope h4').text('Items');
		$('#sidebar #filter div.scope .all').text('All');
		$('#sidebar #filter div.scope .sale').text('Sale');
		$('#sidebar #filter div.scope li *:contains("Available Items")').parent().remove();
		$('#sidebar #filter div.size h4').text('Sizes');
		$('#sidebar #filter div.size .all *').text('All');
		$('#sidebar #filter div.size h5:contains("Bottoms")').text('Pants/Shorts');
		$('#sidebar #filter div.size h5:contains("Tops")').text('Shirts/Jackets');
		$('#sidebar #filter div.size h5:contains("Accessories")').text('Shoes');
		$('#sidebar #filter div.color h4').text('Color');
		$('#sidebar #filter div.color .all *').text('All');
	
		// Add classes
		$('#sidebar #filter strong').parent().addClass('selected');
		$('#sidebar #filter.opened > div').addClass('expanded');
		$('#sidebar #filter ul li').hover(
	
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
	
		);
		$('#sidebar #filter ul').hover(
		
			function() {
				$(this).find('strong').parent('li').addClass('deselected');
			},
			function() {
				$(this).find('strong').parent('li').removeClass('deselected');
			}
		
		);
	
		// Break up sizes into smaller columns
		$('div.size li').each(function(i) {
		
			var length = $(this).children().text().length;
			var nextLength = $(this).next().children().text().length;
			
			if ((length < 8) && (nextLength < 8)) {
			
				$(this).css('width', '70px');
			
			}
		
		});
	
		// Collapse filter categories
		collapse('#sidebar #filter h4');

		// Add See More button
		/* $('#sidebar #filter div.group ul').each(function() {
		
			$listing = $(this);
			
			if ($listing.children('li').length > 9) {
	
				$listing.after('<a href="#" class="seemore">See More</a>');
				$listing.siblings('a.seemore').click(function() {
				
					$(this).siblings('ul').addClass('expanded');
					$(this).hide();
				
					return false;
				
				});
			
			}
		
		}); */

		// Filter overlay
		$('#filter a').click(function() {
	
			loadPage($(this));
			
			return false;

		});
	
	}


/* FUNCTION: STORE SIDEBAR / SEARCH
----------------------------------------------------------------------------- */

	function search() {

		// Search field placeholder
		$('div.search label input').val("Search for ...").attr("title", "Search for ...");
	
		function clearSearchField() {
	
			var label = $(this).attr("title");
	
			if (label == $(this).val()) {
	
				$(this).val("");
	
			} else if ($(this).val() == '') {
	
				$(this).val(label);
	
			}
	
		}
	
		$('div.search ul.actions input').click(function() {
	
			$("div.search label input").each(clearSearchField);
	
		});
		$("div.search label input").blur(clearSearchField);
		$("div.search label input").click(clearSearchField);
	
	}
	

/* FUNCTION: STORE SIDEBAR
----------------------------------------------------------------------------- */

	function sidebar() {

		collections();
		filter();
		// search();

		// Readjust overlay height
		$('#sidebar div.overlay').height($('#sidebar').height());
		
	}
	
	
/* FUNCTION: Clothes Horse
----------------------------------------------------------------------------- */

	function clothesHorse() {
		
		if ($('#clothes-horse').length > 0) {
		
			var clothesHorse = $('#clothes-horse');
			$('label.size').append(clothesHorse);
		
		}
		
	}
	

/* FUNCTION: Twitter
----------------------------------------------------------------------------- */

	function tweets() {
		
		$("#twitter div.tweets").tweet({
			
			username: "fivefour",
			count: 3
			
        });
		
	}
