// JavaScript Document
var venuesSelectedObj = new Object();
var locationSelected = false;
//if($("#location").attr('value')!= '') locationSelected = true;
var selectedGeo = 0;
var selectedVenue = 0;
var searchStr = '';
var geoDetected ='';
var pageTypeRef = 'searchresults';
var geoLocation = 0;

$(document).ready(function(){
 	var pageUrl = window.location.href;
	if(pageUrl.match('locationsdropown') && (($(".refineResultsArea").length>0)|| ((($(".refineResultsArea").length == 0) && ($("#leftColForHeight").length > 0)))))
	{
	var currDate = new Date();
	var presentMonth = currDate.getMonth() + 1 ;
	if (presentMonth <10)
		presentMonth = '0' + presentMonth;
	var presentDate = currDate.getDate();
	if (presentDate <10)
		presentDate = '0' + presentDate;
	var presentDateFormatted = currDate.getFullYear() + '-' + presentMonth + '-' + presentDate;	
	var endMonth = currDate.getMonth();
	var endYear = currDate.getFullYear();
	if (currDate.getMonth() > 6)
	{
		endMonth = (endMonth + 7) % 12;
		endYear = endYear + 1;
	}
	if (endMonth <10)
		endMonth = '0' + endMonth;
	
	var endDateFormatted = endYear + '-' + endMonth + '-' + presentDate;
	
	var noLoactionSelectedErrorDisplayed = false;
	var clickedOnVenuesErrorLayer = true;
	var locationLayerDisplayed = false;
	var clickedOnLocationLayer = false;
	var clickedOnLocationAnchors = false;
	var venueLayerDisplayed = false;
	var clickedOnVenueLayer = false;
	var tabsLayerDisplayed = false;
	var clickedOnTabsLayer = false;
	
	//$.fn.truncateValueString($("#location option:selected").html(), $("#locations"), 12);
	var locationFromCF = $.fn.replaceAll(unescape($.fn.getQueryVariable(window.location.href,'location')),'+',' ');
	$("#location").attr('value',locationFromCF);
	$("#locdropdown").attr('title', $("#location option:selected").html());
	$('#locationdropdowninput').hide();
	$('#locdropdown').show();
	$('#venuesdropdowndiv').show();
	
	$('#startDateCal').datepicker({dateFormat: "mm/dd/yy"}).val(presentDateFormatted);
	$('#endDateCal').datepicker({dateFormat: "mm/dd/yy"}).val(endDateFormatted);
	
	/* Add CF before the Refne Search Results*/
	if($(".refineResultsArea").length > 0)
	{
		$(".cflg").insertBefore(".refineResultsArea");
		$(".cflg").show();
		$(".filterArea .filterLabel").each(function(){
			if($(this).html().match('Date Range'))
				$(this).parent().hide()
		});
	}
	else if($("#leftColForHeight").length > 0)
	{
		$(".cflg").insertBefore("#leftColForHeight");
		$(".cflg").attr("style",'display:block;border-bottom:1px solid #8AB1D7');
		$("#leftColForHeight").hide();
	}
	$('#locations').attr('value',unescape($.fn.getQueryVariable(window.location.href,'locationsTextComplete')));
	$("#locdropdown").attr('title', $.fn.replaceAll($('#locations').attr('value'),'+',' '));
	$.fn.truncateValueString($.fn.replaceAll($('#locations').attr('value'),'+',' '), $('#locations'), 30);
	
	$('#venues').attr('value',unescape($.fn.getQueryVariable(window.location.href,'venuesTextComplete')));
	$("#venuesdropdowndiv").attr('title', $.fn.replaceAll($('#venues').attr('value'),'+',' '));
	$.fn.truncateValueString($.fn.replaceAll($('#venues').attr('value'),'+',' '), $('#venues'), 30);
	//hack

	if ($.fn.getQueryVariable(window.location.href,'searchStr')!= '')
	{
		$('.searchStrCF').attr('value',unescape($.fn.getQueryVariable(window.location.href,'searchStr')));
		$('.searchStrCF').attr('value',$.fn.replaceAll($('.searchStrCF').attr('value'),'+',' '));
	}
	var startDate = unescape($.fn.getQueryVariable(window.location.href,'startDateCal').replace('+', ' '));
	startDate = startDate.split('-');
	$('#startDateCal').attr('value',startDate[1] + '/' + startDate[2] + '/' + startDate[0]);
	var endDate = unescape($.fn.getQueryVariable(window.location.href,'endDateCal').replace('+', ' '));
	endDate = endDate.split('-');
	$('#endDateCal').attr('value',endDate[1] + '/' + endDate[2] + '/' + endDate[0]);
	
	$("#locations").click(function()
	{	
		//locationSelected = true;
		$.fn.showDropDownLayer($('#locations'),$('#locationdropdowndiv'));
		$('#venuesdropdownlayer').hide();
		locationLayerDisplayed = true;
		clickedOnLocationLayer = true;
		selectedVenue = 0;
		$("#venues").attr('value', 'All venues');
		$("#venues").attr('title', 'All venues');
		$("#venuesdropdowndiv").attr('title', 'All venues');
	});
	
	$("#venues").click(function()
	{	if($("#location").attr('value')!= '') locationSelected = true;
		if (locationSelected == false)
		{
			$.fn.showDropDownLayer($('#venues'),$('#venuesErrorLayer'));
			noLoactionSelectedErrorDisplayed = false;								
		}
		else
		{
			$.fn.showDropDownLayer($('#venues'),$('#venuesdropdownlayer'));			
			venueLayerDisplayed = true;
			clickedOnVenueLayer = true;
		
		}
	});

	
	$(".locationanchor").click(function()
	{		
		locationSelected = true;
		$.fn.truncateValueString($(this).html(), $('#locations'), 25);
		$("#locdropdown").attr('title', $(this).html());
		$("#location").attr('value', $(this).attr('name'));
		$("#locationdropdowndiv").hide('slow');
		selectedGeo = $(this).attr('id');
		geoLocation = $(this).attr('id');
		clickedOnLocationAnchors = true;
	});
		/* Venues Selection on click of each venue */
	$(".venuesanchor").livequery('click', function(event)
	{
		$("#venuesdropdownlayer").hide('slow');
		$.fn.truncateValueString($(this).html(), $('#venues'), 25);
		$('#venues').attr('value',$('#venues').attr('value').replace('&amp;','&'));
		$("#venuesdropdowndiv").attr('title',$(this).html());
		if ($(this).attr('id') == geoLocation)
		{
			$("#location").attr('value', $(this).attr('id') + '; ' +  $("#locdropdown").attr('title')); 
			selectedGeo = geoLocation;
			selectedVenue = 0;
		}
		else if($(this).attr('id') != geoLocation)
		{
			selectedGeo = $(this).attr('id');
			selectedVenue = $(this).attr('name');
		}
		else
		{
			selectedGeo = geoLocation;
			selectedVenue = 0;
		}
		clickedOnVenueLayer = true;
	});
	
	$("#cancelvenues").click(function()
	{
		$("#venuesdropdownlayer").hide('slow');
		for (var i in venuesSelectedObj)
			{
				delete venuesSelectedObj[i];
			}
		$("#venuescontentinner div input:checkbox").each(function(){
			$(this).attr('checked',false);
		});      
		$("#venues").attr('value','All venues');
	});
	
	$("#venueslayerokbutton").click(function()
	{	
		var venuesSelectedObjLength = 0;
		for (var i in venuesSelectedObj){
			venuesSelectedObjLength ++;
			}
		if (venuesSelectedObjLength == 1)
		{
			var venuesSelectedObjValue ='' ;
			for (var i in venuesSelectedObj)
				venuesSelectedObjValue = venuesSelectedObj[i];
			$.fn.truncateValueString(venuesSelectedObjValue, $('#venues'), 30);
			$("#venuesdropdowndiv").attr('title',venuesSelectedObjValue);
		}
		else if(venuesSelectedObjLength > 1)
			{
				$("#venues").attr('value','Multiple venues');
				$("#venuesdropdowndiv").attr('title',$("#venues").attr('value'));
			}
		else
			{
				$("#venues").attr('value','All venues');
				$("#venuesdropdowndiv").attr('title',$("#venues").attr('value'));
			}
		$('#venuesdropdownlayer').hide('slow');
		
		for(var i in venuesSelectedObj)
		{
			searchStr = searchStr + ' ' + venuesSelectedObj[i];
		}		
	});
	
	$(".find_btn").click(function()
	{
		if ($('.searchStrCF').attr('value')== 'Artist name (optional)')
		$('.searchStrCF').attr('value','');
		
		var venueValue = '';
		if($("#venuesdropdowndiv").attr('title') != 'All venues') 
			venueValue = $("#venuesdropdowndiv").attr('title');
		var searchStr = '';
		var locationSelected = '';
		if(!($("#locdropdown").attr('title').match('Select location')))
			locationSelected = $("#locdropdown").attr('title');
		$.fn.omnitureTracking("Concert Finder Search", $('.searchStrCF').attr('value'), venueValue, locationSelected);	
			
		//$.fn.omnitureTracking("Concert Finder Search", $('.searchStrCF').attr('value'), $("#venuesdropdowndiv").attr('title'),$("#location").attr('value'));
		
		var locationTextComplete = '<input type="hidden" name="locationsTextComplete"  id="locationsTextComplete" value="'+ $("#locdropdown").attr('title') + '"/>';
		$(locationTextComplete).insertBefore($(".find_btn"));
		
		var venuesTextComplete = '<input type="hidden" name="venuesTextComplete"  id="venuesTextComplete" value="'+ $("#venuesdropdowndiv").attr('title') + '"/>';
		$(venuesTextComplete).insertBefore($(".find_btn"));
		var geoLocationSelected = '<input type="hidden" id="selectedGeo" name="selectedGeo" value="'+geoDetected+'"/>';
		$(geoLocationSelected).insertBefore($(".find_btn"));
		$("#selectedGeo").attr('value',geoLocation);
		if ((selectedVenue != 0) && ($("#venues").attr('value') != 'All venues')) 
			$("#location").attr('value',selectedVenue);
		var startDate = $('#startDateCal').attr('value');
		startDate = startDate.split('/');
		$('#startDateCal').attr('value',startDate[2] + '-' + startDate[0] + '-' + startDate[1]);
		var endDate = $('#endDateCal').attr('value');
		endDate = endDate.split('/');
		$('#endDateCal').attr('value',endDate[2] + '-' + endDate[0] + '-' + endDate[1]);	

		if ($('.searchStrCF').attr('value')== 'Artist name (optional)')
			$('.searchStrCF').attr('value','');
		//$('.searchStrCF').attr('value',$('.searchStrCF').attr('value') + searchStr);		
	});
			
	$('.venuescheckbox').livequery('click', function(event) {
		var id = '#'+ $(this).attr('id');
			if ($(id).is(":checked"))
				venuesSelectedObj[$(this).attr('id')] =  $(this).attr('name');
			else
				delete venuesSelectedObj[$(this).attr('id')];
    }); 
			
	$('#venuesErrorLayer').livequery('click', function(event) {
		$('#venuesErrorLayer').show();
		clickedOnVenuesErrorLayer = true;
    }); 
    	
	$('#locationdropdowndiv').livequery('click', function(event) {
		clickedOnLocationLayer = true;
		if(!(clickedOnLocationAnchors))$('#locationdropdowndiv').show();
		clickedOnLocationAnchors = false;		
    });
	    
    $('#venuesdropdownlayer').livequery('click', function(event) {
		$('#venuesdropdownlayer').show();
		clickedOnVenueLayer = true;
    });
    		
	$('body').livequery('click', function(event) {
		if (noLoactionSelectedErrorDisplayed && !(clickedOnVenuesErrorLayer)) 
			$('#venuesErrorLayer').hide('slow'); 
		if (locationLayerDisplayed && !(clickedOnLocationLayer)) 
			$('#locationdropdowndiv').hide('slow'); 
		if (venueLayerDisplayed && !(clickedOnVenueLayer)) 
			$('#venuesdropdownlayer').hide('slow'); 
		noLoactionSelectedErrorDisplayed = true;
		clickedOnVenuesErrorLayer = false;
		locationLayerDisplayed = true;
		clickedOnLocationLayer = false;
		venueLayerDisplayed = true;
		clickedOnVenueLayer = false;
    }); 
	}	
});
			
;(function($){

	$.fn.showDropDownLayer = function(source, target)
	{		
		target.css({left:source.position().left, top:source.position().top + 25 , position: 'absolute', display:'block'});
	}
})(jQuery);

;(function($){

	$.fn.truncateValueString = function(valueText, target, length)
	{	
		if(valueText.length > 25)
			target.attr('value', valueText.substring(0,length) + '...');
		else
			target.attr('value', valueText.substring(0, length + 3));
	}
})(jQuery);

;(function($){

	$.fn.omnitureTracking = function(eVar49Prop26, searchStr, venueStr, locationStr)
	{	
    var s=s_gi(s_account);                      
    s.linkTrackVars="eVar9,eVar11,eVar10,eVar49,prop12,prop13,prop14,prop26";          
	s.eVar9=searchStr;
	s.eVar10=venueStr;
	s.eVar11=locationStr;
	s.eVar49=eVar49Prop26;
	s.prop12=searchStr;
	s.prop13=venueStr;
	s.prop14=locationStr;
	s.prop26=eVar49Prop26;
	s.event32=searchStr;
	s.event33=searchStr;         
    s.tl(this,'o','Find');
	}
})(jQuery);
// Query string Parser

;(function($){
	$.fn.getQueryVariable = function(query,variable) {
  var query = query;
  query = query.substring(query.indexOf('?') + 1); // remove everything up to the ?
  query = query.replace(/\&$/, ''); // remove the trailing &
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
} 
})(jQuery);

;(function($){
	$.fn.replaceAll= function(str, pcFrom, pcTo){
	var i = str.indexOf(pcFrom);
	var c = str;
	
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
}
})(jQuery);

