var transparentImage = "images/none.gif";

var reloadflag = 0;

function fixTrans()
{
	if (typeof document.body.style.maxHeight == 'undefined') {

	var imgs = document.getElementsByTagName("img");
	
	for (i = 0; i < imgs.length; i++)
	{	
		if (imgs[i].src.indexOf(transparentImage) != -1)
		{
			return;
		}

		if (imgs[i].src.indexOf(".png") != -1)
			{
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "block";
			}
		}	
	}
}

if (document.all && !window.opera){	attachEvent("onload", fixTrans);}

var replaceus = new Array('.hotel-rates-box label','.sub-nav li a','.page2 .sub-nav h4 span','.btm-box h4 span','.hotel-rates-box h3 span','.menu li a', '.class-infobox h2');

// document.write("<style>" + replaceus.join(',') + " {visibility: hidden}</style>")


$(document).ready(function() {	
	/* hide stupidly id'd element on create new event page */
    $("#button0").css("background-color", "orange");
	$('[id^=Featured_Event]').hide();
	$('#Short_title label').html('Title (required):');

	/* change 24hr clock to 12hr on create new event page */
	$('.ors-attribute-help-text').hide();	// '24 hour format'...not anymore!

	// add the ampm select
	var ampmSelect	=	'<div class="element">'
			+		'<label>&nbsp;</label>'
			+		'<select id="ampm" name="ampm">'
			+			'<option value="AM">AM</option>'
			+			'<option value="PM">PM</option>'
			+		'</select>'
			+	'</div>';
	$('.time').each(function(){
		$(this).append(ampmSelect);
	});
$('#subnav_filter').submit(function(){
	$('#ajax_offset').val(0);
});

	// fix values on submit
	$('#editform').submit(function(){
		if ($('#From_Time #ampm').val() == 'PM' && (parseInt($('#From_Time .time input:first').val()) < 12))
			$('#From_Time .time input:first').val(parseInt($('#From_Time .time input:first').val()) + 12);

		if ($('#To_Time #ampm').val() == 'PM' && (parseInt($('#To_Time .time input:first').val()) < 12))
			$('#To_Time .time input:first').val(parseInt($('#To_Time .time input:first').val()) + 12);

		return true;
	});

	/* get rid of the div elements that wrap the input elements */
	/* $('#form_6 ul li div input.element').unwrap(); */
	/* alert($('#form_6 ul li div input.element').length + ' form elements on this page.\n'); */
	
	/* get rid of the li elements that wrap the labels and form inputs */
	/* $('#form_6 ul li label.description').unwrap();*/
	/* alert($('#form_6 ul li label.description').length + ' form labels on this page.'); */
	
	
	$("#form_6 li").each(function(){
		target = $("#form_6 .form_description");
		if ($(this).attr('id') != 'li_captcha' && $(this).attr('id') != 'li_buttons') {
			$(this).children().each(function(){$(this).appendTo(target)});
			$(this).remove();
		}
	})

/*	
	$('#form_6 #saveForm').click(function(){return tailgateForm();});
	
	function tailgateForm(e){
		alert('What a tip!');
		return false;
	}
*/

$('#form_6').submit(function() {
  /*var formDataSerialized = $(this).serialize();*/
  /*alert(formDataSerialized);*/
  /*$.post('/createEZobject/post_create_object', $(this).serialize());*/
  $.post('/createEZobject/post_create_object.php', $(this).serialize());
  return true;
});


	$.history.init(callback);
	
	if ($('.home_map')) $('.home_map').click(function(){document.location.href='/Getting-Here'});

	$('#saveForm').css({'background' : 'url(/extension/site/design/site/images/submitform.png) no-repeat', 'width' : '110px', 'height' : '33px', 'cursor': 'pointer', 'border' : 'none', 'text-indent' : '1000px', 'overflow' : 'hidden'});
	$('#saveForm').parent().append('<input id="clearForm" class="reset" type="reset" value="" />');
	$('#clearForm').css({'background' : 'url(/extension/site/design/site/images/clearform.png) no-repeat', 'width' : '110px', 'height' : '33px', 'cursor': 'pointer', 'border' : 'none'});
	
	
	Cufon.replace('.hotel-rates-box label', { fontFamily: 'UniversCondensed' });
	Cufon.replace('.sub-nav li a', {hover: true, fontFamily: 'Rosewood'});
	Cufon.replace('.page2 .sub-nav h4 span', { fontFamily: 'Rosewood' });
	Cufon.replace('.class-infobox h2, #form_container h2', {hover: true, fontFamily: 'Rosewood'});
	Cufon.replace('.btm-box h4 span', { fontFamily: 'Rosewood' });
	Cufon.replace('.hotel-rates-box h3 span', { fontFamily: 'Rosewood' });
	Cufon.replace('.menu li a', {hover: true, fontFamily: 'Rosewood'});

	
// 	for (i=0;i<replaceus.length;i++) {
// 		$(replaceus[i]).css('visibility','visible');
// 	}

	
	var box_h = document.getElementById('widget-box');
	if(box_h){
		var _btn = box_h.getElementsByTagName('a');
		var _list = box_h.getElementsByTagName('li');
		for(var i = 0; i < _btn.length; i++){
			if(_btn[i].className.indexOf('close') != -1){
				_btn[i].onclick = function(){
					box_h.className = box_h.className.replace('opened','');
					for(var i = 0; i < _list.length; i++){
						_list[i].className = _list[i].className.replace('active','');
					}
					return false;
				}
			}
			else if(_btn[i].parentNode.tagName == 'LI'){
				_btn[i].onclick = function(){
					if(box_h.className.indexOf('opened') == -1) box_h.className += ' opened';
					for(var i = 0; i < _list.length; i++){
						_list[i].className = _list[i].className.replace('active','');
					}
					this.parentNode.className += ' active';
				}
			}
		}
	}
});

var view_majors_on_html = 'Collapse Ongoing Events';
var view_majors_off_html = 'Expand Ongoing Events';
var majors_on = 0;

function majors_controls(on) {
	if (on) {
		$('#close_majors').show();
		$('#close_majors2').show();
		$('#view_majors').html(view_majors_on_html);
		$('#view_majors').attr('href', 'javascript: void(0);');
		$('#view_majors').css({'background-image': 'url(/extension/site/design/site/images/arrow-expanded.png)'});
		document.getElementById('cal_month_majors').style.height = 'auto'; 
		majors_on = 1;
	} else {
		$('#close_majors').hide();
		$('#close_majors2').hide();
		$('#view_majors').html(view_majors_off_html);
		$('#view_majors').attr('href', 'javascript: void(0);');
		$('#view_majors').css({'background-image': 'url(/extension/site/design/site/images/arrow-contracted.png)'});
		document.getElementById('cal_month_majors').style.height = '55px'; 
		majors_on = 0;
	}
}

function calCufon(){
	Cufon.replace('#major_box h2', {fontFamily: 'Rosewood', fontSize: '32'});
	Cufon.replace('#display_date_wrap_bsw h2, #display_date_wrap h2', {fontFamily: 'Rosewood', fontSize: '20px'});
	Cufon.replace('.dateback', {fontFamily: 'Rosewood', fontSize: '16px'});
	Cufon.replace('.cal_line label', {fontFamily: 'Rosewood'});

	// initialize majors controls
	majors_controls(majors_on);

	$('#view_majors').click(function(){
		majors_controls(!majors_on);
	});
	$('#close_majors').click(function(){
		majors_controls(0);
	});
	$('#close_majors2').click(function(){
		majors_controls(0);
	}); 
	

	$('#search_events').attr('value', '').click(function(){
		if ($('#search_query').val() == 'Search Text') {
			$('#search_query').val('');
		}
	});
}

function initBrowser(){
	if (navigator.appVersion.indexOf("Safari") != -1){
		if (!window.getComputedStyle) document.body.className += " safari";
			else document.body.className += " safari3";
		}
}

if (window.addEventListener){
	window.addEventListener("load", initBrowser, false);
}

function poi_search() {
	var target = 'ajax_target';
	if (arguments[0]) target = arguments[0];
	var qstring = '?';
	$('#subnav_filter .sendme').each(function(){
		qstring += $(this).attr('name') + '=' + $(this).getValue().replace(/ /g, '+') + '&';
	});
	qstring += 'target=' + target;
	var rnode = parseInt($("select[name='custom_select']").getValue());
	
	if (isNaN(rnode)) rnode = 2;
	
	var target = '/layout/set/poi_search/content/view/full/'+ rnode + qstring;
	$.history.load(target.replace(/^.*#/, ''));
	return false;
}

function callback(hash) {
	if (hash) {
		var target = hash.split('target=')[1]
		$("#"+target).load(hash, function(responseText, textStatus, XMLHttpRequest){if (textStatus == 'success') paginate(target);});
		vals = reversehash(hash);
		if (vals) {
			$("select[name='town_select']").setValue(vals[1]);
			$("select[name='custom_select']").setValue(vals[0]);
		}
		reloadflag = 1;
	} else if (reloadflag) {
		location.reload(true);
	}
}

paginate = function(target) {
	offset = parseInt($("#ajax_offset").getValue());
	limit = parseInt($('#ajax_limit').getValue());
	html = "<div class='pagwrapper'><div class='pagination'>";
	total = parseInt($('#ajax_results').attr('searchcount'));
	if (offset > 0) { 
		html += "<a class='next' href='javascript: offsetjump(" + (offset - limit) + ", \"" + target + "\")'>Prev</a>"; 
	} else {
		html += "<span class='next'>Prev</span>";
	}
	var i = 0;
	var myclass = '';
	while (i*limit < total) {
		myclass = ''
	if (i*limit == offset) myclass = " class='current'";
		html += "<a " + myclass + "href='javascript: offsetjump(" + (i*limit) + ", \"" + target + "\")'>" + (i+1)  + "</a>"
		i++;
	}
	if (offset < (total - limit)) {
		html += "<a class='prev' href='javascript: offsetjump(" + (offset + limit) + ", \"" + target + "\")'>Next</a>"; 
	} else {
		html += "<span class='prev'>Next</span>";
	}
	html += "</div</div>";
	$('#'+target).append(html);
}


offsetjump = function(offset, target) {
	$("#ajax_offset").setValue(offset);
	poi_search(target);
}

function reversehash(hash) {
	tmp = hash.split('/full/');
	if (tmp.length > 1) {
		rb = tmp[1];
		tmp = rb.split('/(town)/');
		if (tmp.length > 1) {
			return new Array(tmp[0], tmp[1]);
		}
	}
	return false;
}


function reset_poi_search() {
	$("select[name='town_select']").setValue("All"); 
	if (arguments[0]) $("select[name='custom_select']").setValue(arguments[0]); 
	$("#pr_from_date").setValue("mm/dd/yyyy"); 
	$("#pr_to_date").setValue("mm/dd/yyyy"); 
	$("#from_date").setValue("mm/dd/yyyy"); 
	$("#to_date").setValue("mm/dd/yyyy"); 
	$("#keywords").setValue(""); 
	$("#pr_keywords").setValue(""); 
}

function dbcustomcal(id, date) {
	$('#'+id).attr('value', date);
}

function showDatePicker( base, id, datatype ) {
    var calIconID = base + '_' + datatype + '_cal_' + id;
    var calContainerID = base + '_' + datatype + '_cal_container_' + id;
    var calContainer = document.getElementById( calContainerID );

    var xy = YAHOO.util.Dom.getXY( calIconID );

    calContainer.style.left = ( xy[0] + 26 ) + 'px';
    calContainer.style.top = ( xy[1] -450 ) + 'px';
    calContainer.style.display = 'block';

    window['cal'+id] = new YAHOO.widget.Calendar( base + '_' + datatype + '_cal_' + id , calContainerID, { close: true, 
                                                                                              mindate: "1/1/1970",
                                                                                              LOCALE_WEEKDAYS: "medium" } );
    window['cal'+id].render();
    window['cal'+id].selectEvent.subscribe( handleSelect, window['cal'+id], true );
}

$(document).ready(function(){

	//reservation widget ------------------------------------------
	/*
	//home page - expanded
	$('#left_col .book_content:first').show();
	$('#left_col .section_name:first').addClass('on');
	//Stay section - expanded
	if (url[3] == "Stay" || url[3] == "Dine" || url[3] == "Play") {
		$('.book_content:first').show();
		$('.section_name:first').addClass('on');
	}
	*/
	
	$(".section_name").click(function(){
		if ($(this).next('.book_content').is(':hidden')) {
			$('.book_content').hide();
			$('.section_name').removeClass('on');
			$(this).addClass('on');
			$(this).next('.book_content').show();
		} else {
			$('.book_content').hide();
			$('.section_name').removeClass('on');
		}
		return false;
	});
	$('.book_content:first').show();
	$('.section_name:first').addClass('on');
});
