/* JASON ALDEAN */
/* Javascript Functions
----------------------------------------------------------------------------- */


/* POST-LOAD FUNCTION
----------------------------------------------------------------------------- */

	$(window).load(function() {
	
        $('#footer div.widget-alert').appendTo('#title'); 

		// if (page == "index" || page == "home" || section == "cmafest") CMAFest();
        

/* RANDOM BACKGROUNDS
----------------------------------------------------------------------------- */

        var random = Math.floor(Math.random()*6) + 1;
        $('body').addClass('bg' + random);
	

/* MEMBER NAVIGATION DROPDOWN
----------------------------------------------------------------------------- */

        $('ul#navigation li:has(ul)').hover(function() {
    
            $(this).addClass('hover');
    
        }, function() {
    
            $(this).removeClass('hover');
    
        });

		
/* CLIQUE SIGN IN
----------------------------------------------------------------------------- */
	
        if (page == 'login') {
    
            // START PACKAGE TIER SLIDESHOW
            $('ul.tiers').before('<div id="paging">').cycle({
    
                fade: true,
                speed: 400,
                timeout: 10000,
                pager: '#paging',
                before: function() { if (window.console) console.log(this.src); }

            });

            // CUSTOM PAGING LINKS
            $('#paging a:nth-child(1)').livequery(function() { $(this).addClass('online'); });
            $('#paging a:nth-child(1)').livequery(function() { $(this).text('Online Only Membership'); });
            
            $('#paging a:nth-child(2)').livequery(function() { $(this).addClass('basic'); });
            $('#paging a:nth-child(2)').livequery(function() { $(this).text('Big Green Tractor Membership'); });
            
            $('#paging a:nth-child(3)').livequery(function() { $(this).addClass('deluxe'); });
            $('#paging a:nth-child(3)').livequery(function() { $(this).text('Big Green Tractor Deluxe Membership'); });
                    
        }


/* SITE-ONLY
----------------------------------------------------------------------------- */

        if (section != "messageboard" && section != "checkout" ) {
        
        	$('#mailing_list_entry_email').cliqueForms({ placeholder: "Enter Your Email Address" });

            // PLUGIN: CLIQUE ACTIONS (Replaces submit buttons with text links)
            try { $('input[type=submit]').cliqueActions(); } catch (error) { alert('Error: Missing Clique Actions Plugin'); }

            if (page == "index" || page == "news" || page == "home") {
        
                readmore();
                $('div.widget-event.featured ul.actions li a.action.browse').attr('href','upcoming');
        
            }
            
            if (page == 'index' || page == 'home') {
                
                banners();
                events();
                
                $('#page-index.mykindaparty li.buy a').click(function() {
                
                    return false;
                
                });
                
            }
            
            if (page == 'upcoming') {
            
                $('td.tickets ul li.presale').parents('tr').addClass('presale');
                $('<p class="ticketmaster"><strong>Ticketmaster Pre-sale</strong></p>').prependTo("div.widget-event.browse tr.ticketmaster.presale td.tickets");
                $('<p class="non_ticketmaster"><strong>Non Ticketmaster Pre-sale</strong></p>').prependTo("div.widget-event.browse tr.non_ticketmaster.presale td.tickets");
            
            }

			if (page == 'event') rsvp();
        
            killTabs();
            tabs();
            cufon();
            paging();
            truncate();
            comments();
            wording();	

            if (section == "client") biography();

            if (section == "tour") sponsorship();

            if (section == "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();
                
            }
	

/* END SITE-ONLY
----------------------------------------------------------------------------- */

	}


/* END POST-LOAD FUNCTIONS
----------------------------------------------------------------------------- */

	});


/* FUNCTION: TOUR SPONSORSHIP
----------------------------------------------------------------------------- */

	function sponsorship() {

		// Sponsor in Tour Listing
		if (page == "upcoming" || page == "past") {
			
			$('<p class="cotton_states"><em>Presented by</em> <strong>Cotton States</strong></p>').prependTo("div.widget-event.browse tr.cotton_states td.tickets");
			$('<p class="trips_picks"><em>Presented by</em> <strong>Trips and Picks</strong></p>').prependTo("div.widget-event.browse tr.trips_and_picks td.tickets");
			$('<p class="sponsor"><em>Presented by</em> <strong>Country Financial</strong></p>').prependTo("div.widget-event.browse tr.country_financial td.tickets");
			
		}
		
		// Sponsor in Event
		if (page == "event") {
			
			$('<p class="cotton_states"><em>Presented by</em> <strong>Cotton States</strong></p>').insertAfter("div.widget-event.display.cotton_states h4.date");
			$('<p class="trips_picks"><em>Presented by</em> <strong>Trips and Picks</strong></p>').insertAfter("div.widget-event.display.trips_and_picks h4.date");
			$('<p class="sponsor"><em>Presented by</em> <strong>Country Financial</strong></p>').insertAfter("div.widget-event.display.country_financial h4.date");
							
		}
		
	}

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

	function cufon() {
	
		var tradegothic_medium = [
			
			'div.widget-subscribe h3',
			'div.widget-account.setup h3',
			'div.widget-cart.modify h3',
			'#page-photos #scope div.widget-tag h3',
			' div#answers div h3',
			'.program .events li em',
			'.program .events li strong abbr',
			'#sidebar #departments li a',
			'#benefits h4',
			'div.ticketmaster h4',
			'div.checkin h4',
			'#page-login div.help h4',
			'#page-login div.disclaimers h4',
			'#page-update #page h4',
			'#page-account #page h4',
			'#page-contests #sidebar h4',
			'#page-meetgreet #content h4',
			'.section-legal #content h4',
			'div.gift.pane h3',
			'a.action.another',
			'p.landing.info',
			'p.steps.info',
			'.step-list p',
			'.success.message p',
			'.warning p'
		
		];

		var tradegothic_bold = [ 
		
			'h2',
			'h3:not(div.widget-subscribe h3, div.widget-account.setup h3, div.widget-account.signin h3, div.widget-cart h3, div#answers div h3, #page-photos #scope div.widget-tag h3, div.gift.pane h3)',
			'#page-welcome div.group > h3',
			'h4:not(#biography h4, div.widget-order.receipt h4, #features ul.photos h4, #benefits h4, #page-login div.ticketmaster h4, #page-login div.checkin h4, #page-login div.help h4, #page-login div.disclaimers h4, #page-update #page h4, #page-account #page h4, #page-contests #sidebar h4, #page-meetgreet #content h4, .section-legal #content h4)',
			'#biography h4',
			'.headlines li a:not(a.comments, a.action, div#answers div li a)',
			'a.action:not(#page-login a.action)',
			'ul.pages a',
			'ul.pages strong',			
			'div.program ul.tickets li a',
			'.section-tour td.tickets a',
			'.tabs li a',
			'.program .events li strong span',
			'#navigation li a:not(#navigation ul.member li a)',
			'.banner',
			'#contest fieldset em a'

			
		];

		Cufon.replace(tradegothic_medium, { fontFamily: 'TradeGothic_Medium', hover: true });
		Cufon.replace(tradegothic_bold, { fontFamily: 'TradeGothic_Bold', hover: true });
		
		$(tradegothic_medium.toString()).css('visibility', 'visible');
		$(tradegothic_bold.toString()).css('visibility', 'visible');
						
	}


/* FUNCTION: MOVE "READ MORE" LINK
----------------------------------------------------------------------------- */ 

	function readmore() {

		$('div.widget-editorial.browse ul.leads > li').each(function() {

			var link = $(this).find('a.display').attr('href');
			$(this).find('ul.actions').remove();

			// IF LAST CHILD IS P.LEAD
			if ( $(this).find('div.story > *:last-child').hasClass('lead') ) {

				$(this).find('div.story p.lead:last').append('&nbsp;&nbsp;<a class="readmore" href="' + link + '">Read More...</a>');

			}

			// IF LAST CHILD IS ANOTHER ELEMENT
			else {

				$(this).find('div.story > *:last-child').after('<br /><a class="readmore" href="' + link + '">Read More...</a>');

			}

		});

	}


/* FUNCTION: KILL TABS
----------------------------------------------------------------------------- */ 

	function killTabs() {

		// REMOVE EMPTY JAVASCRIPT TAB AND PANE
		$('div.tabbed').not(".static, .ajax").children('ul.tabs').children('li').each(function() {

			var pane = $(this).children('a').attr('href');

			if ( $(this).parent().parent().find(pane).children('*').length == 0 ) {

				$(this).remove();
				$(this).parent().parent().find(pane).remove();

			}

		});

		// REMOVE TABBED PANEL
		$('div.tabbed').not(".static, .ajax").children('ul.tabs').each(function() {

			if ($(this).children('li').length == 0) {

				$(this).parent().remove();

			}

		});

	}


/* FUNCTION: INITIALIZE TABS
----------------------------------------------------------------------------- */

	function tabs() {

		// FEATURES BOX TABS
		if (page == 'index' || page == 'home') {

			$('div#features > ul.tabs').tabs({

				fxSlide: false,

				show: function() {
				
					Cufon.refresh();

					// Photos is the first tab, begin cycling
					if ( $('#photos').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#photos" ) {
										
						$('div#features > ul.tabs').tabs();
										
						$('#photos').addClass('pane').show();
					
						photoSlideshow();
						
						if ($('div#features div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {
		
							$('div#features div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(
		
								function() {
		
									$('a#photos_back').show();
									$('a#photos_next').show();
		
								}, function() {
		
									$('a#photos_back').hide();
									$('a#photos_next').hide();
		
								}
		
							)
						
						} 				
						
					}
					
					// Radio is the first tab, begin cycling
					else if ($('#radio').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#radio" ) {
										
						$('div#features > ul.tabs').tabs();
		
						$('#radio').addClass('pane').show();
		
						photoSlideshow();
		
						if ($('div#features #radio div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {
		
							$('div#features #radio div.widget-photo.slideshow div a, a#radio_back, a#radio_next').hover(
		
								function() {
									
									$('a#radio_back').show();
									$('a#radio_next').show();
		
								}, function() {
									
									$('a#radio_back').hide();
									$('a#radio_next').hide();
		
								}
		
							)
						}
					}
					
					else {
		
						// PHOTOS TAB
						if ($('#photos:visible').length > 0) {
								
							$('#video div.flash div').empty();
							$('#radio div.flash div').empty();
	
							photoSlideshow();
	
							$('div#features div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(
			
								function() {
										
									$('a#photos_back').show();
									$('a#photos_next').show();
	
								},
								function() {
		
									$('a#photos_back').hide();
									$('a#photos_next').hide();
	
								}
	
							)
	
						} 
	
						// RADIO TAB
						else if ($('#radio:visible').length > 0) {
	
							$('#video div.flash div').empty();
	
							photoSlideshow();
	
							var radio_player = $('#radio div.flash div').attr('id');
							window[radio_player]();
	
						}
	
						// VIDEO TAB
						else if ($('#video:visible').length > 0) {
	
							$('#radio div.flash div').empty();
	
							photoSlideshow();
	
							var video_player = $('#video div.flash div').attr('id');
							eval(video_player)();
	
						}
						
						else {
							
							
						}
		
					}
		
				}

			});

		}

		// JAVASCRIPT TABS
		$('div.tabbed').not("#features, .ajax, .static").children('ul.tabs').tabs({

			fxSlide: false,

			click: function() {
			
				Cufon.refresh();

			}, load: function() {

				truncate();
				Cufon.refresh();

			}, show: function() {

				truncate();
				Cufon.refresh();

			}

		});

		// AJAX TABS
		$('div.tabbed.ajax > ul.tabs').tabs({

			fxSlide: false,

			click: function() {

				loading('start');
				Cufon.refresh();

			}, load: function() {

				truncate();
				loading('stop');
				Cufon.refresh();

			}, show: function() {

				truncate();
				loading('stop');
				Cufon.refresh();

			}

		});

	}


/* FUNCTION: LOADING INDICATOR
----------------------------------------------------------------------------- */

	function loading(event) {

		/*if (event == "start") {

			$('#loading').animate({
			left: '0px'
			}, 300);

		} else {

			$('#loading').animate({
			left: '-54px'
			}, 300);

		}*/

	}


/* FUNCTION: PAGING FOR TABS
----------------------------------------------------------------------------- */ 

	function paging() {

		// AJAX TABS
		$('div.tabbed.ajax ul.paging a').livequery('click', function(event) {

			loading('start');

			$(this).parents('div.pane').load($(this).attr('href'), function(data) {

				loading('stop');

			});

			return false;

		});

		// JAVASCRIPT TABS
		$('div.tabbed:not(.ajax):not(.static) ul.paging a').each(function() {

			var pane = $(this).parents('div.pane').attr('id'); 
			$(this).attr('href', $(this).attr('href') + '#' + pane); 

		});

	}


/* FUNCTION: TRUNCATE TEXT
----------------------------------------------------------------------------- */

	function truncate() {

		// DESCRIPTIONS OF TITLED BROWSE WIDGETS
		$('div#content div.titled strong').each(function() {

			if ($(this).text().length >= 55) {

				var text = $(this).text().slice(0,55);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// FEATURES BOX: PHOTO TITLE
		$('div#features #photos h4').each(function() {

			if ($(this).text().length >= 45) {

				var text = $(this).text().slice(0,45);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});
		
		// MEMBER ALIAS: SPLIT GRID
		$('div.grid.split div#content .member a.alias strong').each(function() {

			if ($(this).text().length >= 15) {

				var text = $(this).text().slice(0,15);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// MEMBER STATUS: SPLIT GRID
		$('div.grid.split div#content .member dd.status').each(function() {

			if ($(this).text().length >= 30) {

				var text = $(this).text().slice(0,30);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});
		
		// MEMBER STATUS: SPLIT GRID
		$('div.grid.split div#content .member dt.location').each(function() {

			if ($(this).text().length >= 13) {

				var text = $(this).text().slice(0,13);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});
		
		// MEMBER STATUS: TWO THIRDS GRID
		$('div.grid.two_thirds div#content .member dd.status').each(function() {

			if ($(this).text().length >= 60) {

				var text = $(this).text().slice(0,60);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// MEMBER ALIAS: TWO THIRDS GRID
		$('div.grid.two_thirds div#content .member a.alias strong').each(function() {

			if ($(this).text().length >= 30) {

				var text = $(this).text().slice(0,30);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// EVENT LISTING ON INDEX PAGE: SHORTEN LOCATION
		$('#page-index div.widget-event td.details').each(function() {

			if ($(this).text().length >= 15) {

				var text = $(this).text().slice(0,15);

				$(this).text(text);
				$(this).append('<var>&hellip;</var>');

			}

		});


	}


/* FUNCTION: COMMENTS
--------------------------------------------------------------------------- */

	function comments() {

		// ADDS CANCEL BUTTON FUNCTIONALITY
		if ($('#comment').siblings('ul.tabs').children().length > 1) {

			$('#comment a.cancel').click(function() {

				$('#comment textarea').val('');
				$('#comment').siblings("ul.tabs").tabsClick(1);
				return false;

			});

		}

		// REMOVES CANCEL BUTTON IF NO COMMENTS EXIST
		else {

			$('#comment a.cancel').remove();

		}

	}
	

/* FUNCTION: CHANGE WORDING
--------------------------------------------------------------------------- */

	function wording() {
	
		$('#page-welcome #title h2').text('Welcome to the Offcial Jason Aldean Fan Club');	
		$('div.widget-alert p:contains(double-check the page below for any missing or wrong information)').text('Please double-check the page for any missing or wrong information');
		$('<a class="readmore">Read More</a>').appendTo('#biography div.story');
		
		// EVENT PAGE CLIQUETOOLS PRE-SALE TICKETS
		$('#page-event div.widget-item.display div.topic > h3').text('UPCOMING PRE-SALE TICKET SALES');
		$('#page-event div.widget-item.display div.purchase ul.actions a.action.submit').text('BUY PRE-SALE TICKETS');
		Cufon.refresh();
		
	 	if (topic == "july_21_2011" || topic == "july_21_2011-1" || topic == "january_28_2011" || topic == "january_28_2011-3") {
		
			$('#page-event div.widget-event.display > div.tickets').remove();
			$('#page-event div.widget-event.display > ul.actions li a.presale').remove();
		
		} 

		// HIDE CONTEST ALERT
		$('body.section-cmafest div.widget-alert.error h3:contains("There was a problem entering this contest.")').parent().remove();
		
	}
		

/* FUNCTION: BIOGRAPHY TOGGLE
--------------------------------------------------------------------------- */
	
	function biography() {

		$('#biography div.story p:not(.lead)').wrapAll('<div class="more"></div>');

		$('#biography a.readmore').toggle(function() {
			
			$('#biography div.story div.more').slideDown('500');
			$(this).text('Read Less');
			return false;
					
		}, function() {
		
			$('#biography div.story div.more').slideUp('500');
			$(this).text('Read More');
			return false;
			
		});
	
	}
		

/* FUNCTION: SHOW UPCOMING EVENTS IF THERE ARE NO FEATURED EVENTS
----------------------------------------------------------------------------- */

	function events() {
	
		if ($('div.widget-event.featured').length > 0) {
		
			$('div.widget-event.upcoming').hide();
			
		}
		
		else {
		
			$(this).parent('div.widget-event.featured').hide();
			$('div.widget-event.upcoming').show();

		}
		
	}


/* FUNCTION: CMA FEST
----------------------------------------------------------------------------- */

	function CMAFest() {

		if (browser['class'] == 'msie' && browser['version'] >= parseInt('9.0')) {

			// Show content for IE9 browser
			$('.content').css('visibility', 'visible').css('height', 'auto').css('overflow', 'visible');

			// Home page call to action
			if (page == 'index' || page == 'home') {

				var $alert = $('<div class="widget-alert info single cmafest"></div>');
		
				// Site mode
				if (ie9['sitemode']) {
				
					if (signed_in) {
						$alert.append("<div><h3>Have you entered <strong>Microsoft's<br />CMA Fest Pin To Win Contest</strong> today?</h3></div><ul class='actions'><li><a class='action cmafest' href='/cmafest-steps?step=2'>Enter Everyday to Increase Your Chances of Winning</a></li></ul>");
					} else {
						$alert.append("<div><h3>Have you entered <strong>Microsoft's<br />CMA Fest Pin To Win Contest</strong> today?</h3></div><ul class='actions'><li><a class='action cmafest' href='/cmafest'>Enter Everyday to Increase Your Chances of Winning</a></li></ul>");
					}
	
				// Not site mode
				} else {
	
					$alert.append("<div><h3>Enter to win 4-day passes to CMA Fest and exclusive merchandise in <strong>Microsoft's CMA Fest Pin To Win Contest</strong>!</h3></div><ul class='actions'><li><a class='action cmafest' href='/cmafest'>Enter Now</a></li></ul>");
	
				}

				$('#title').prepend($alert);
			
			}

			// Steps page
			if (page == "cmafest-steps") {

				$('.step-list li').hover(function() {
				
					$('.step-list li').removeClass('active');
					$(this).addClass('active');
					
				});
		
				$('.step-list li.step1').hover(function() {
				
					$('.step-image').removeClass('step2 step3');
					$('.step-image').addClass('step1');
					
				});
				
				$('.step-list li.step2').hover(function() {
				
					$('.step-image').removeClass('step1 step3');
					$('.step-image').addClass('step2');
					
				});
				
				$('.step-list li.step3').hover(function() {
				
					$('.step-image').removeClass('step1 step2');
					$('.step-image').addClass('step3');
					
				});
			
			}

			// Success page
			if (page == "cmafest-success") {
	
				// Site mode
				if (ie9['sitemode']) {

					var $form = $('div.success form');
					var pageVal = $form.children('input[name="page"]').val();
					var topicVal = $form.children('input[name="topic"]').val();
					var tagVal = $form.children('input[name="tag"]').val();
					var successVal = $form.children('input[name="contest[success_page]"]').val();
					var sigVal = $form.children('input[name="contestsuccess_page_sig"]').val();
					var submitVal = 'Enter+Contest';
					var agreeVal = '1';
					var postURL = '/process/contest/display' + '?page=' + pageVal + '&topic=' + topicVal + '&tag=' + tagVal + '&contest%5Bsuccess_page%5D=' + successVal + '&contestsuccess_page_sig=' + sigVal + '&submit=' + submitVal + '&agree=' + agreeVal;

					$.post(postURL, function(data) {

						var $alert = $(data).find('div.widget-alert');
		
						if ($alert.hasClass('success')) {
		
							$('div.success div.splash').css('visibility', 'visible').css('height', '400px').css('overflow', 'visible');
		
						} else if ($alert.hasClass('info')) {
		
							$('div.success').html($alert);
							$('div.success div.widget-alert').show();
		
						} else {

							$('div.success div.widget-alert').show();
		
						}
						
					});
		
				// Not site mode
				} else {
		
					$('div.success').html($('<div class="widget-alert error single"><h3>You need to install the Pin Site to continue</h3><p><a href="' + site_host + '/cmafest-steps">Follow these steps</a> to successfully enter the contest.</p></div>'));
					$('div.success div.widget-alert').show();
		
				}
			
			}
	
		} else {
		
			// Show unsupported browser warning
			$('.warning').show();
		
		}
		
	}
	
/* FUNCTION: RSVP + CONTEST ENTRY
----------------------------------------------------------------------------- */

	function rsvp() {
		
		$('#page-event #contest label em a:contains("Learn More")').text('Enter Now');

		if ($('#contest fieldset').length > 0) {

			$('#contest').show();

		}

	}
