/* SNOOP DOGG STORE TEMPLATE */
/* Javascript Triggers and Custom Functions
----------------------------------------------------------------------------- */



	$(document).ready(function() {

		tungsten = [
			
			'ul.tabs a',
			'ul.items dt:first-child a',
			'ul.actions a:not(#clique a)',
			'a.action:not(#clique a)',
			'#departments a',
			'#title h2',
			'#page h3',
			'div.display > h4',
			'#billboard h3',
			'#header .social .label',
			'ul.pages a',
			'ul.pages strong'
		
		];
		
		$(tungsten.toString()).css('visibility', 'hidden');
		
		browser();
		markupChanges();
		billboard();

		if (section == "help") {			
			
			//Taken from http://oncemade.com/animated-page-scroll-with-jquery
			$('a.scrollpage').click(function() {
  			var elementClicked = $(this).attr("href");
   			var destination = $(elementClicked).offset().top;
   			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
  			return false;
			});
		
		}

	});
	
	$(window).load(function() {
	
		try { $('input[type=submit]').cliqueActions(); } catch (error) { alert('Error: Missing Clique Actions Plugin'); }
		if (section != "checkout") tabs('removeEmptyTabs');
		if (section == "store") store();

		cufon();
		social();
		
		// Facebook opt-in demo

        $('div.widget-subscribe.facebook').each(function() {

            var link = $('div.widget-subscribe.facebook a').attr('href');

    		$('#optin a').attr('href', link);
    		$('#optin input').hide();
    		$(this).hide();
    		$('#optin').parent().removeAttr('action');
    		$('#optin a').unbind();
        
        });
		
	});

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


	function store() {		
	
		// SUB-DEPARTMENTS
		$('#subcategories').each(function() {
	
			var category = $('#subcategories h3').text();
			$('#subcategories ul').find('a:contains("Everything")').parent().remove();
			$('#subcategories ul').appendTo('#departments li:has(a:contains("' + category + '"))');
			$('#subcategories').remove();
	
		});
		
		// PLUGIN: CLIQUE FORMS
		try {
		$('.widget-item.search input#query').cliqueForms({ placeholder: "Search Store" });
		$('.widget-item.search input#query').css('visibility', 'visible'); // Will not chain to previous function?
		$('#mailing_list_entry_email').cliqueForms({ placeholder: "Enter Your Email Address" });
		$('#mailing_list_entry_mobile').cliqueForms({ placeholder: "Enter Your Mobile Number" });
		} catch (error) { alert('Error: Missing Clique Forms Plugin'); }

		// PLUGIN: CLIQUE ITEM WIDGET
		try {
		$('div.widget-item.display:not(.photos, .gift)').cliqueItemWidget({

			checkStock: false,
			pressure: false,
			browser: 'thumbnails', // thumbnails / slideshow
			removeEmptyThumbs: true, // Removes rows of empty thumbnails
			viewer: true, // Shows enlarged images
			preview: false, // Preview enlarged images when hovering thumbnails
			zoom: 'drag' // loupe / drag / none

		});
		$('.widget-item.browse.tooltip').cliqueItemWidget();
		$('.widget-item.filter').cliqueItemWidget();
		} catch (error) { alert('Error: Missing Clique Item Plugin'); }

		// PLUGIN: CLIQUE ALERT WIDGET
		// try { $('div.widget-alert.added').cliqueAlertWidget(); } catch (error) { alert('Error: Missing Clique Alert Plugin'); }

		// PLUGIN: CLIQUE UPLOADER
		if (page == "item" && $('#review').length > 0) { try { $('#review').cliquePhotoUploader(); } catch (error) { alert('Error: Missing Clique Uploader Plugin'); } }
					
		// MAILING LIST
		$('#optin ul.actions li input.save').attr('value','Get Updates');
		$('#optin ul.actions li a.submit').text('Get Updates');
		
		// CART
		$('div#cart span:contains("You have")').remove();
		
	}


/* FUNCTION: CUFON REPLACEMENTS
----------------------------------------------------------------------------- */


	function cufon() {

		Cufon.replace(tungsten, { hover: true });
		$(tungsten.toString()).css('visibility', 'visible');
				
	}


/* FUNCTION: BROWSER DETECTOR
----------------------------------------------------------------------------- */


	function browser() {
			
		if (navigator.userAgent.match('MSIE')) {
		
			browser = "ie";
			
		} else if (navigator.userAgent.match('Firefox')) {
		
			browser = "gecko";
			
		} else if (navigator.userAgent.match('AppleWebKit')) {
		
			browser = "webkit";
		
		}
		
		$('body').addClass(browser);

	}
	
	
/* FUNCTION: SOCIAL NETWORK LINKS
----------------------------------------------------------------------------- */


	function social() {
	
		$('ul.social a').hover(function() {
		
			var name = $(this).text();
			$(this).parent().siblings('.label').text(name);
			Cufon.refresh();
			
		}, function() {
		
			$(this).parent().siblings('.label').html('');
		
		});
	
	}
	

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


	function markupChanges() {

		$('#footer div.widget-alert').appendTo('#title');
		$('#optin').append('<p>Join the mailing list to receive updates via email and/or text messages.</p>');
		$('div.widget-account a.submit').text('Save Changes');
		$('div.purchase li.notify a.submit').text('Notify When In Stock');
		$('#signin a.action.submit').text('Sign In');
		$('#signin div.widget-account.password a.action.submit').text('Reset Password');
		$('#page-signin #signin div.widget-account.password a.action.submit').text('Reset');
		$('#page-shipping #addressbook a.action.submit').text('Use This Address');
		
		// Move Item Listing Prices
		$('ul.items dt:contains($)').each(function() {
		
			var price = $(this).html();
			$(this).parent().prev().append('<span>' + price + '</span>');
			$(this).remove();
				
		});
		
		// Large Product Thumbnail
		$('#page-store ul.items li:first-child img').each(function() {
		

		});
	
	}
	
	
/* FUNCTION: BILLBOARD
----------------------------------------------------------------------------- */


	function billboard() {

		// GET BANNER FEED
		$.get('/feeds/banners', function(data) {

			$(data).find('unit').each(function() {

				if ($(this).find('name').text() == "billboard") {

					var slides = "<ul>";

					$(this).find('banner').each(function() {

						if ($(this).children('status').text() != "Retract Temporarily") {

							var link = $(this).children('url').text();
							var image = $(this).children('image').text();
							
							slides += '<li><a href="' + link + '"><img src="' + image + '" /></a></li>';
						
						}

					});

					slides += "</ul>";
					
					$('#slides').append(slides);

				}
				
			});

			// SLIDESHOW
			$('#slides ul').after('<div class="pager"></div>').cycle({

				fx: 'fade',
				speed: 1000,
				timeout: 4000,
				pager: '#slides .pager'

			});
			
			// MAKE PAGER HIT AREA LARGER
			$('#slides .pager a').each(function() { $(this).html('<span>' + $(this).text() + '</span>'); });

		});
		
	}

