/* TICKETMIX */
/* Functions
----------------------------------------------------------------------------- */


	$(window).load(function() {


/* CITY SELECTOR
----------------------------------------------------------------------------- */


	var city_tag = $.cookie("ticketmix_city");

	$('div.widget-tag ul li:first-child').children().text('All Cities')
	$('div.widget-tag ul li:first-child').appendTo('div.widget-tag ul');

	$('div.widget-tag ul li strong').each(function() {

		$(this).children('a').insertAfter(this);

	});
	
	$('div.widget-tag strong').remove();

	if (city_tag && city_tag.length > 0) {
	
		$('div.widget-tag ul li a').each(function() {
		
			if ($(this).attr('href').match(city_tag)) {
				$(this).wrap('<strong></strong>');
			}
		
		});

	} else {

		$('div.widget-tag ul li:last-child a').wrap('<strong></strong>');
	
	}
	
	$('div.widget-tag ul li strong').parent().addClass('selected').prependTo('div.widget-tag ul');

	if (city_tag && city_tag != 'all') {
	
		var bottle_service = $('#navigation a.bottle').attr('href', '/events/' + city_tag + '-Bottle_Service');
	
	}

	$('div.widget-tag ul li strong').click(function() {
	
		parent.location = '/index';	
	
	});

	$('div.widget-tag').show();


// Set Cookie


	var cookie_host = site_host.replace('http://','');

	$('div.widget-tag ul li a').click(function() {

		$(this).attr('target_','_top');
		var tag = $(this).attr('href').split('/')[2];
		$.cookie("ticketmix_city", tag, { expires: 365, domain: cookie_host, path: '/' });

	});


// Insert City Name from Selection


	var city_text = $('div.widget-tag ul li strong').text();

	// $('span.city').text(city_text);


// Hover State


	$('#navigation div.widget-tag').hover(function() {
	
		$(this).addClass('hover');
	
	}, function() {
	
		$(this).removeClass('hover');
	
	});
	
	$('#navigation div.widget-tag li:not(:first-child)').hover(function() {
	
		$(this).addClass('hover');
	
	}, function() {
	
		$(this).removeClass('hover');
	
	});


/* EVENT LISTINGS
----------------------------------------------------------------------------- */


// Change Markup and Inject Featured Markup


	$('a.tickets').removeAttr('target');
	
	$('div.widget-event ul.events > li').each(function() {

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

		if (link.match('rsvp') && $(this).find('a.tickets').length == 0) {

			$(this).find('ul.actions').append('<li><a class="action rsvp_only" href="' + link + '">RSVP Only</a></li>');

		}

		if (page == "events" && link.match('featured')) {

			$(this).addClass('featured');
			$(this).clone().prependTo('div.widget-event ul.events');
			$(this).remove();

		}
		
		if ($(this).parent().parent().attr('id') == 'featured') {
			$(this).addClass('featured');
		}
		
		if ($(this).find('img').length > 0) {
			$(this).find('a.details').remove();
		}

	});

	$('div.widget-event table tr').each(function() {

		var venue = $(this).children('td.details:not(.action.details)').text();
		var link = $(this).find('a.details').attr('href');

		if (link && link.match('rsvp') && $(this).find('a.tickets').length == 0) {

			$(this).find('td.action').next().remove();
			$('<td class="action rsvp"><a class="action rsvp_only" href="' + link + '">RSVP Only</a></td>').insertAfter($(this).find('td.action'));

		}


		$(this).children('td.action.details').removeClass('details').addClass('info');
		$(this).children('td.location').append('<br /><em>' + venue + '</em>');
		$(this).children('td.details').remove();

	});
	
	$('div.bottle_service p').text('Sorry, no events in this area are currently offering Bottle Service').show();

	$('div.widget-event table').show();
	$('div.widget-event ul.events').show();


// Remove Top 8 Tag from Event and Bottle Service Links


	if ($('div.widget-event a.browse').length > 0) {

		var browse_page = $('div.widget-event a.browse').attr('href');
		var browse_page = browse_page.replace('-Top_8','');
		$('div.widget-event a.browse').attr('href', browse_page);

	}

	if ($('a.bottle').length > 0) {

		var bottle_service = $('a.bottle').attr('href');
		var bottle_service = bottle_service.replace('-Top_8','');
		var bottle_service = bottle_service.replace('Featured-','');
		var bottle_service = bottle_service.replace('-Bottle_Service-Bottle_Service','-Bottle_Service');
		$('a.bottle').attr('href', bottle_service);

	}


// Setup Event Header


	var venue = $('#content dd.venue').text();
	$('<em class="venue">' + venue + '</em>').insertBefore('h4.date');


// Expand Clickable Area


	$('div.widget-event ul.events > li h4, div.widget-event ul.events > li strong, div.widget-event ul.events > li em').click(function() {

		window.location = $(this).siblings('ul.actions').children('li').children('a.details').attr('href');
		
	});

	$('div.widget-event table tr').click(function() {

		window.location = $(this).children('td.info').children('a').attr('href');
	
	});


/* TICKET PURCHASE
----------------------------------------------------------------------------- */

	
	if ($('div.added').length > 0) {
	
		var event = $('div.widget-item div.topic h3').text();
		var quantity = $('div.widget-alert.added h3').text().slice(0,2);
	
		$('div.widget-alert.added h3').text(quantity + ' Tickets to ' + event + ' have been added to your cart');
		$('a.continue').parent().remove();
	
	} else if ($('div.error').length > 0) {
	
		var heading = $('div.error h3').text();
		var heading = heading.replace('purchases of this item','purchases of these tickets')
		var advice = $('div.error p').text();
		var advice = advice.replace('this item','tickets to this event')

		$('div.error h3').text(heading);
		$('div.error p').text(advice);
	
	}

	
/* EVENT PAGE
----------------------------------------------------------------------------- */


	if ($('#masthead a.tickets').length == 0) {

		$('#masthead div.discontinued h4').text('Tickets to this event have sold out');
		$('#masthead div.confirmation').show();

		$('#masthead div.confirmation li.toggle label').click(function() {
		
			$(this).children('input').attr('checked','checked');
			$('#masthead div.confirmation input[type=submit]').click();
		
		});

		try {
		
			$('div.confirmation ul.toggle').cliqueForms()
		
		} catch(missing) {}

		$('#masthead div.confirmation > label').wrap('<li class="guests"></li>');
		$('#masthead div.confirmation > li').appendTo('#masthead ul.toggle');
		
		if ($('#masthead div.confirmation ul.toggle li:first-child input').attr('checked') == true) {
			$('#masthead div.confirmation ul.toggle li:first-child').hide();
			$('#masthead div.confirmation li.guests').show();
		} else {
			$('#masthead div.confirmation ul.toggle li:nth-child(2)').hide();
		}

		$('#masthead div.confirmation li.guests select').change(function() {
		
			$('#masthead div.confirmation input[type=submit]').click();
		
		});
		
		$('div#masthead div.widget-event.modify').show();


		// Add RSVP Message
		
		if ($('#masthead div.confirmation p').length > 0) {
			$('#masthead div.confirmation p').wrap('<div class="rsvp"></div>');
			$('#masthead div.confirmation div.rsvp').prependTo('#content');
		}
	
	}


/* SIGN IN PAGE
----------------------------------------------------------------------------- */


	if (page == 'login') {
	
		$('div.widget-alert').appendTo('div.signin').show();

		$('a.reset').insertAfter('input[type=password]');
		$('a.reset').wrap('<small></small>').show();
		$('a.reset').click(function() {

			$('div.signin').hide();
			$('div.password').show();
			return false;
		
		});

		$('div#register div').cycle({

			fx: 'blindX',
			speed: 400,
			timeout: 6000
		
		});

	}
	

/* CART PAGE
----------------------------------------------------------------------------- */


	$('div.widget-cart h3').each(function() {

		var heading = $(this).text();	
		var heading = heading.replace('items','tickets').replace('item','ticket');
		
		$(this).text(heading);
	
	});
	
	$('#cart_success_page').each(function() {
	
		var checkout = $('div.widget-cart a.checkout').attr('href');
		$(this).val(checkout);
	
	});
	

/* RECEIPT PAGE
----------------------------------------------------------------------------- */

	
	$('a.print').click(function() {
	
		window.print();
		return false;
	
	});


/* POST PAGE LOAD FUNCTIONS
----------------------------------------------------------------------------- */	
	
	
	});