var saveCenterH = 0;
$(document).ready(function() {

	var core_domain = 'irvinecvb';
	var baseURL = '/';
	saveCenterH = $("#center_column").height();
	
	//resizeCenter();

//***************************************************************************
// Ra-ccordion

	var multiple_open = 'FALSE'; 	
	var accordion_states = new Object;	
	var types = new Array('popular', 'travelocity', 'special_packages', 'calendar');
	var state = new Array('0', '1', '0', '0');
	
	
	accordion_states['popular'] = 0;
	accordion_states['travelocity'] = 1;
	accordion_states['special_packages'] = 0;
	accordion_states['calendar'] = 0;
	
	//create holders and establish
	for(var i in types){
		//accordion_states[types[i]] = 0;
		target = '#accordion_' + types[i];
		
		if(accordion_states[types[i]] == 0){
			$(target).hide();
		}
	}
	
	//Resize the columns
	resizeCenter();
	//if(saveCenterH < $("#container_right_column").height())
		//$("#center_column").css({'height': $("#container_right_column").height()});
	
	$(".accordionHeaderPlus").click(function(){
		target =  '#accordion_' + $(this).attr('contains');
		if(accordion_states[$(this).attr('contains')] == 0){
			$(this).removeClass('accordionHeaderPlus').addClass('accordionHeaderMinus')		
			$(target).slideDown('fast');
			accordion_states[$(this).attr('contains')] = 1;			
			if(multiple_open == 'FALSE')
			{
				for(var i in accordion_states)
				{
					if(i != $(this).attr('contains')){ 
						$('#accordion_' + i).slideUp('fast');
						$('#' + i).removeClass('accordionHeaderMinus').addClass('accordionHeaderPlus')
						accordion_states[i] = 0};
				}			
			}
		}else{
			$(this).removeClass('accordionHeaderMinus').addClass('accordionHeaderPlus')
			$(target).slideUp('fast');
			accordion_states[$(this).attr('contains')] = 0;		
		}
		
		setTimeout("resizeCenter()", 500);
	});	

//***************************************************************************
// Newsletter Form Sign Up
	function EmailOK(myEmail)
	{
	 // var re = /([a-z0-9]+([-_]?[a-z0-9]+)*.)*[a-z0-9]+([-_]?[a-z0-9]+)*@([a-z0-9]+([-_]?[a-z0-9]+)*.)+[a-z]{2,4}/gi;
	  //return (document.getElementById(myEmail).value.match(re) == document.getElementById(myEmail).value);  
	  
		//return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(myEmail);
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return reg.test(document.getElementById(myEmail).value);
	}
	$('#submitNewsletter').click(
		function() {
			if((($('#newsletterEmail').val()).length) == 0)
				alert("Please insert an e-mail address");
			else
			{
				if(EmailOK('newsletterEmail') == false)
					alert("Please insert a valid e-mail address");
				else
				{
					$.ajax({
						url: baseURL + "signup/newsletter/",
						type: 'POST',
						data: 'email='+$('#newsletterEmail').val(),
						dataType: 'html',
						timeout: 8000,
						error: function(){alert('Error: impossible to register.');},
						success: function(html){
							alert(html);
							$('#newsletterEmail').val("");
						}
					});
				}
			}
	});


	//*******************************************
	// input validator function
	function setGrey(selector, message){
		$(selector).css("color", "grey");
		$(selector).val(message);
		$(selector).focus(function(){
			if(this.value == message){
				this.value = '';
				$(selector).css("color", "black");
			}
		});

		$(selector).blur(function(){
			if (this.value == ''){
				this.value = message;
				$(selector).css("color", "grey");
			}	
		});
	}
	//*******************************************
	
	//filthy is url external thing
	$('a').each(function(){
		var test = $(this).attr('href').split('irvinecvb');
		if(test.length == 1){ 
			var fullurl = $(this).attr('href').split('http');
			if(fullurl.length != 1){
				$(this).attr('target', '_blank');				
			}
		};
	})
	
		
	//*******************************************
	// Search Live
	function searchSet(selector, message){
		$(selector).val(message);
		$(selector).focus(function(){
			if(this.value == message){
				this.value = '';
			}
		});

		$(selector).blur(function(){
			if (this.value == ''){
				this.value = message;
			}	
		});
	}

	searchSet('.searchFieldInput', 'search here');
	
	$('.searchFieldInput').keyup( 
			function(e) {
				var searchItem =$(this).val();
				if(searchItem.length < 3){ $('#results').html(' '); return; }
					if(e.keyCode == 13 ){					
						document.location= baseURL + 'search/page/' + searchItem;
					}
					//alert($('#searchField').val());				
					$.ajax({
					    url: baseURL + "search/results/" ,
					    type: 'POST',
						data: "searchQuery="+ searchItem,
					    dataType: 'html',
					    timeout: 2000,
					    error: function(){
					       
					    },
					    success: function(html){
							$('#results').show();
					       	$('#results').html(html);
							searchClick();
					    }
				});
									
			});
			$('#mouseTip').hide();

//******************************************************************************************************************
	$('.sideItem').click(function(){
		document.location=$(this).attr('link');
	})
	
	function searchClick(){
		$('.resultBodyInner').click(function(){
			document.location=$(this).attr('link');
		})
		
		$('.resultBodyInnerStart').click(function(){
			document.location=$(this).attr('link');
		})	
			
		//Resize the columns
		if($("#center_column").height() < $("#container_left_column").height())
			$("#center_column").css({'height': $("#container_left_column").height()});
	}
	
	//******************************************************************************************************************
	// event name, type, description - DEPRECATED
				var eventArray = new Object();
				var rss_location = baseURL + 'services/rss/events';

				$.ajax({
					    url: rss_location,
					    dataType: 'xml',
					    timeout: 8000,
					    error: function(){
					        //alert('could not load events feed :-(');
					    },
					    success: function(xml){

							event_type = rss_location.split("/");
							event_holder = event_type[event_type.length-1];

							$('item', xml).each(function(){					


									// date aquisition and formatting
									date = $(this).children('date').text().split('-')
									if(date[2].charAt(0) == 0){date[2] = date[2].substr(1, 1);}	

									// tool tip setup
									link_event_id = $(this).children('link').text().split("/");
									eventArray[link_event_id[link_event_id.length-1]] = $(this).children('title').text(); //+ '<br><hr>' + $(this).children('description').text();	
							});										
						}				
					});



				$("a.cal").mouseover(function(e) { 
							var stringText = this.toString();
							stringTextarray = stringText.split("/");
							//set the text of the roll over
							$('#mousetipInner').html(eventArray[stringTextarray[stringTextarray.length-1]]);    
						var cssObj = {
								position: "absolute",
								left: e.pageX,
								top: e.pageY,
								color: "rgb(255, 255, 255)",
						        backgroundColor: "rgb(232,129,37)",
								height: '50px',
								width: '200px'
						      }
							$('#mouseTip').css(cssObj);
							$('#mouseTip').show('fast');
					} 
				);

				// hide rollover on mouse out
				$("a").mouseout( function() {
							$('#mouseTip').hide('Slow');
							isShown = 0;			
					} 
				);

				// reposition with mouse movement
				$("a").mousemove( function(e) {

					var cssObj = {
							position: "absolute",
							left: e.pageX + 20,
							top: e.pageY,
							zIndex: 5,
					        color: "rgb(0,40,244)"
					      }
					$('#mouseTip').css(cssObj);	
					} 
				);

				$('#contact_form_button').click(function(){

					if( ($('#contact_name').val() == "")||($('#contact_email').val() == "")||($('#contact_subject').val() == "")||($('#contact_message').val() == "") )
							alert("Please fill out all the fields");
					else
					{		
						var what = $('#contact_email').val().split('@');
						if(what.length < 2){
							alert('please enter a valid email address');
							return;
						}else{
							var second = what[1].split('.');
								if(second.length < 2){
									alert('please enter a valid email address');
									return;
								}
						}

						var query_string = 		
						'firstname=' + $('#contact_name').val() + '&' +	
						'email=' + $('#contact_email').val() + '&' +
						'subject=' + $('#contact_subject').val() + '&' +
						'message=' + $('#contact_message').val() + '&';		
						$.ajax({
							url: baseURL + "signup/contact/" ,
							type: 'POST',
							data: query_string,
							dataType: 'html',
							timeout: 1000,
							error: function(){

							},
							success: function(html){
							   alert(html);
							$('#contact_name').val('');	
							$('#contact_email').val('') ;
							$('#contact_subject').val(''); 
							$('#contact_message').val('') ;
							}
						});
					}
				})

		
//
//Tourism Date Fields

$('#date1').click(function(){
	$(this).select();
})
$('#date2').click(function(){
	$(this).select();
})	
		
	//bug report form
	$('#bugs_form_button').click(function(){
											 
		if($('#description').val() == "")
				alert("Please enter a bug description");
		else
		{					 							 
			var query_string = 		
			'userName=' + $('#userName').val() + '&' +	
			'userEmail=' + $('#userEmail').val() + '&' +
			'summary=' + $('#summary').val() + '&' +
			'description=' + $('#description').val() + '&' +
			'os=' + $('#os').val() + '&' +
			'osVersion=' + $('#osVersion').val() + '&' +
			'browser=' + $('#browser').val() + '&';
			$.ajax({
				url: baseURL + "bugs/report/" ,
				type: 'POST',
				data: query_string,
				dataType: 'html',
				timeout: 1000,
				error: function(){
					
				},
				success: function(html){
				   	alert(html);
					$('#userName').val('');	
					$('#userEmail').val('') ;
					$('#summary').val(''); 
					$('#description').val('') ;
				}
			});
		}
	})					
				
});

function resizeCenter()
{
	/*
	//Resize the columns
	if(saveCenterH < $("#container_right_column").height())
		$("#center_column").css({'height': $("#container_right_column").height()});
	else
		$("#center_column").css({'height': saveCenterH});
		*/
		
	//Resize the columns
	var changeH = 0;
	
	if($("#center_column").height() < $("#container_left_column").height())
		changeH = $("#container_left_column").height();
	else
		changeH = $("#center_column").height();
		
	var rightClmnH = 200 + $('#newsletterRegistration').height() + 200;
	if($('#accordion_travelocity').is(':visible'))
		rightClmnH += $('#accordion_travelocity').height() + $('#travelocity').height() + 20;
		
	if($('#accordion_special_packages').is(':visible'))
		rightClmnH += $('#accordion_special_packages').height() + $('#special_packages').height() + 20;
		
	if($('#accordion_calendar').is(':visible'))
		rightClmnH += $('#accordion_calendar').height() + $('#calendar').height() + 20;
		
	if(changeH < rightClmnH)
		changeH = rightClmnH;


//alert($("#container_left_column").height()+" // "+$("#center_column").height()+" // "+rightClmnH+" == "+changeH);
	/*
	$("#container_right_column").css({'height': changeH/2});
	$("#container_left_column").css({'height': changeH/2});
	$("#center_column").css({'height': changeH/2});*/
	//alert($('#center2_container').height()+" // "+changeH);
	$("#center_column").css({'height': changeH});
	
	if($.browser.msie && jQuery.browser.version == '6.0')
		$('#top_of_page').css({'margin-top': '150px'});
}