$(document).ready(function() {
	// Header Animation
	for (i = 2; i <= pictures; i++) {
		$('.athmo').append("<img alt=\"Athmo " + i + "\" src=\"/img/athmo/athmo-" + i + "\.jpg\" style=\"display:none;\" />");
	}

	$('.menu ul:first').after("<div class=\"athmo-pager\"></div>");
	
	$('.athmo').cycle({
		timeout:6000,
		pager:'.athmo-pager:first'
	});
	
	$('.athmo-pager a').each(function(index, element) {
		$(this).addClass("tooltip").attr("title", string_pager + " " + ( index + 1 ));
	});
	
	if ($.cookie("athmo") == "false") {
		$('.athmo').cycle("pause");
		var title = string_play;
	} else var title = string_pause;
	
	$('.athmo').after("<a class=\"athmo-control tooltip\" title=\"" + title + "\"><span class=\"invisible\">" + title + "</span></a>");
	
	$('.athmo-control').click( function(e) {
		e.preventDefault();
		
		$.cookie("athmo", $.cookie("athmo") == "false" ? true : false, { path:"/", expires:7 });
		
	    qtip = $(this).qtip("api");
	    qtip.updateContent( ( title == string_play ? title = string_pause : title = string_play ) );
		
	    $('.athmo').cycle("toggle");
	})
	
	// Tooltips
	$('a.tooltip').qtip();
	
	// Fancyboxes
	$("a.highslide-image").fancybox({ "zoomSpeedIn":300, "zoomSpeedOut":300, "centerOnScroll":false, "imageScale":false }); 
	$(".gallery a.highslide-image").attr("rel", "gallery").fancybox({ "zoomSpeedIn":300, "zoomSpeedOut":300, "centerOnScroll":false, "imageScale":false, titlePosition:"inside" });
	
	// Google Map
	if ($("#map-locator").length) {
		var latlng = new google.maps.LatLng(coordinates[0], coordinates[1]);
	    
	    var map = new google.maps.Map(
	    	document.getElementById("map-locator"),
    		{
    			zoom: 15,
    			center: latlng,
    			disableDefaultUI: true,
    			navigationControl: true,
    			mapTypeId: google.maps.MapTypeId.ROADMAP
		    }
	    );
	    
	    var marker = new google.maps.Marker({
	        position:latlng,
	        map:map
	    });
	}
	
	// Image Area Selects
	if ($("#image-to-crop").length) {
		$("img#photo_users, img#photo_letter").imgAreaSelect({ minWidth: 120, minHeight: 120, aspectRatio: "1:1", handles: true, x1: 10, y1: 10, x2: 120, y2: 120, onSelectEnd: function (img, selection) { $('input[name=x1]').val(selection.x1); $('input[name=y1]').val(selection.y1); $('input[name=x2]').val(selection.x2); $('input[name=y2]').val(selection.y2); }  }); 
		$("img#photo_athmo, img#photo_news_entries").imgAreaSelect({ minWidth: 180, minHeight: 130, aspectRatio: "1.3846153846153846153846153846154:1", handles: true, x1: 10, y1: 10, x2: 190, y2: 130, onSelectEnd: function (img, selection) { $('input[name=x1]').val(selection.x1); $('input[name=y1]').val(selection.y1); $('input[name=x2]').val(selection.x2); $('input[name=y2]').val(selection.y2); }  }); 
		$("img#photo_groups").imgAreaSelect({ minWidth: 240, minHeight: 160, aspectRatio: "1.5:1", handles: true, x1: 10, y1: 10, x2: 250, y2: 170, onSelectEnd: function (img, selection) { $('input[name=x1]').val(selection.x1); $('input[name=y1]').val(selection.y1); $('input[name=x2]').val(selection.x2); $('input[name=y2]').val(selection.y2); }  }); 
	}
	
	// Form Field Mouseovers for Internet Explorer
	if ($.browser.msie) {
		$$("fieldset div").each(function(element, i) {
			element.observe("mouseover", function() {
				element.addClass("input-hover");
			}.bind(this));
			
			element.observe("mouseout", function() {
				element.removeClass("input-hover");
			}.bind(this));
		});
	}
	
	// Create Settings Box
	var settings = $("#settings");
	
	if (settings.length) {
		var settingsPanel = $("#settings-panel"); 
			
		if (!settingsPanel.hasClass("opened")) {
			settingsPanel.hide();
			
			var link = "<a id=\"toggle-settings\" href=\"javascript:void(0);\">" + settings.html() + "</a>";
			settings.html("").append(link);
		
			$("#toggle-settings").click(function () {
				settingsPanel.slideToggle(1000);
		    });
		}
	}
	
	// Add Facebook Scripts
	if ($("#fb-root").length) {
		window.fbAsyncInit = function() {
			FB.init({appId:151476964869538, status:true, cookie:true, xfbml:true});
	  	};

	  	(function() {
	    	var e = document.createElement('script'); e.async = true;
	    		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';

		    $("#fb-root").after(e);
	  	}());
	}
	
	// Page Management
	if ($("#PageContent").length) {
		if ($("#PageContent").attr("checked") == false) {			
			$("#page-content").hide();
		}
		
		$("#PageContent").click(function(event){			
			$("#page-content").toggle(1000);
		});
	}
	
	// Event Management
	if ($("#EventAnnouncement0").length) {
		if ($("#EventAnnouncement1").attr("checked")) {			
			$("#meta").hide();
		}
		
		$("#EventAnnouncement0").click(function(event){	
			$("#meta").slideDown(1000);
		});
		
		$("#EventAnnouncement1").click(function(event){	
			$("#meta").slideUp(1000);
		});
	}
	
	if ($("#LocationChooser0").length) {
		if ($("#LocationChooser0").attr("checked")) {	
			$("#location-new").hide();
			
			var options = $("#LocationLocationId option");
			
			if (!$(options[0]).attr("selected")) {
				$("#rooms").hide();
			}
		} else if ($("#LocationChooser1").attr("checked")) {
			$("#location-existing").hide();
		} else {
			$("#location-new").hide();
			$("#location-existing").hide();
		}
		
		$("#LocationChooser0").click(function(event){		
			$("#location-existing").slideDown(1000);
			$("#location-new").slideUp(1000);
		});
		
		$("#LocationChooser1").click(function(event){		
			$("#location-existing").slideUp(1000);
			$("#location-new").slideDown(1000);
		});
		
		$("#LocationChooser2").click(function(event){			
			$("#location-existing").slideUp(1000);
			$("#location-new").slideUp(1000);
		});
				
		$("#LocationLocationId").change(function(event) {			
			$("#LocationLocationId option").each(function(option) {
				if ($(this).attr("selected") && $(this).attr("value") == 1) {
					$("#rooms").slideDown(1000);
				} else if ($(this).attr("selected") && $(this).attr("value") != 1) {
					$("#rooms").slideUp(1000);
				}
			});
		});
	}
		
	if ($("#EventMusicteam").length) {
		var musician_options = $("#EventMusician option");
		
		musician_options.each(function(index) {
			$("#EventMusicteam").attr("value") ? null : ( this.disabled = true );
		});
		
		$("#EventMusicteam").change(function(event) {			
			musician_options.each(function(index) {
				this.disabled = false;
				this.selected = false;
			});
			
			$.each(window["group_user_" + $(this).attr("value")], function(index, value) { 
				musician_options.each(function(index) {	
					if (this.value == value) {							
						this.selected = true;
					}
				});
			});
		});
	}
	
	// Open Forms in new Window
	if ($(".blank").length) {
		$(".blank").click(function(e){
			this.form.target = "_blank";
		});
	}
	if ($(".deblank").length) {
		$(".deblank").click(function(e){
			this.form.target = "_self";
		});
	}
	
	if ($(".translation").length) {
		var radio = $(".translation input:checked");
		var textarea = radio.parent().next(".textarea");
		
		if (parseInt(radio.attr("value")) == 0) {
			textarea.hide();
		}

		$(".translation input").click(function (event) {
			if ($(this).attr("value") == 0) {
				textarea.slideUp(1000);
			} else textarea.slideDown(1000);
	    });
	}
	
});

preload_area=function(where) {
	$("#" + where).css({ "border-color":"#eeeeee" }).prepend(create_hider(where));
}
create_hider=function(where) {
	return "<div id=\"hider\" class=\"hider\" style=\"width:" + ( $("#" + where).outerWidth() - 2 ) + "px; height:" + ( $("#" + where).outerHeight() - 3 ) + "px;\"><img src=\"/app/webroot/img/sitetech/preloader.gif\" style=\"margin-top:" + ( ( $("#" + where).outerHeight()/2 - 20 ) > 200 ? 200 : $("#" + where).outerHeight()/2 - 20 ) + "px;\" /></div>"
}
preload_area_destroy=function(where) {
	$("#" + where).css({ "border-color":"#ffffff" });
	$("#hider").remove();
}

effect_tablerow=function() {
	$("#elementToBlindDown").slideDown(1000);
}

flash_message=function() {
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

	if (!IE6) {
		if (($("#errorMessage") || $("#noticeMessage"))) {
			kind = $("#errorMessage") ? "#errorMessage" : "#noticeMessage";
			$(kind).addClass("dyn-message").hide().fadeIn(2000);
			window.setTimeout("flash_message_fade('" + kind + "')", 5000);
		}
	}
}

flash_message_fade=function(kind) {
	$(kind).fadeOut(2000);
}

init_tinyMCE=function() {
	tinyMCE.init({ 
		mode:'textareas', 
		theme:'advanced', 
		theme_advanced_buttons1:'bold,italic,separator,bullist,numlist,separator,link,unlink', 
		theme_advanced_buttons2:'', 
		theme_advanced_buttons3:'', 
		theme_simple_buttons:'', 
		plugins:'paste', 
		valid_elements:'ul,ol,li,p,br,b,a[href|target=_blank],i', 
		remove_linebreaks:false, 
		force_br_newlines:true, 
		forced_root_block : '' 
	});
}
