/**
 * Pensimo JavaScript stuff.
 * @author NOSE
 * 
 * @version 1.0.0 initial version
 *
 */
 
	DEBUG = false;
	
var Pensimo = {
	
	/**
	* Parameters.
	*/
	CSVFILE: "notspecified",
	
	/**
	 * Initialize ready.
	 */
	initializeReady : function(){
		// all
		
		// template home
		if (jQuery("#body.home").size() > 0) {
			Pensimo.templateHome();
		}
		
		// template contact 
		if (jQuery("#body.contact").size() > 0) {
			Pensimo.templateContact();
		}
		
		// template appartments
		if (jQuery("#body.appartments").size() > 0) {
			Pensimo.templateAppartments();
		}
		
		// template appartment
		if (jQuery("#body.appartment").size() > 0) {
			Pensimo.templateAppartment();
		}
		
		// template manage
		if (jQuery("#body.manage").size() > 0) {
			Pensimo.templateManage();
		}		
		
		// template application
		if (jQuery("#body.application").size() > 0) {
			Pensimo.templateApplication();
		}
		
		// gallery
		if (jQuery(".gallery").size() > 0) {
			Pensimo.initializeGallery();
		}
		// thickbox
		if (jQuery(".thickbox").size() > 0) {
			Pensimo.initializeThickbox();
		}
		
		
		// template application
		if (jQuery("#body.uptodate").size() > 0) {
			Pensimo.initializeSlideshow();
		}
		
		
		
	},
	/**
	 * Initialize on load.
	 */
	initializeLoad : function(){
		// scroll pane
		if (jQuery(".scrollpane").size() > 0) {
			Pensimo.initializeScrollPane();
		}		
	},
	/**
	* Initializes the home template.
	*/
	templateHome: function() {
		// image strip
	   ImageStrip.initialize();
	},
	/**
	* Initializes the contact template.
	*/
	templateContact: function() {
		
		// init formulat
		$("#formular").validationAideEnable(null, {fieldMessageSeparator: " - ", showInlineMessages:true, showSummary:false });
			// $("#callopt").change( function() {
			$("#callopt").bind("click", function() {
				// $.log("Change Callopt");
				if (this.checked) {
					$("#formPhone").addClass("validator-required");
					$('<span class="required">&nbsp;*</span>').appendTo("label[for='formPhone']");
				} else {
					$("#formPhone").removeClass("validator-required");
					$("#formPhone").removeClass("validation-failed");
					$("span", "label[for='formPhone']").remove();
					$("#ValidationInlineErrorMessage-formPhone").hide();
				}
			});
			
			$("#sendEmail").change( function() {
				if (this.checked) {
						sendFax("no");
						sendPost("no");
						$("#postopt").attr("checked", "checked");
				}
			});
			$("#sendFax").change( function() {
				if (this.checked) {
						sendFax("yes");
						sendPost("no");
						$("#postopt").attr("checked", "checked");
				}
			});
			$("#sendPost").change( function() {
				if (this.checked) {
						sendFax("no");
						sendPost("yes");
						$("#postopt").attr("checked", "checked");
				}
			});
				
			// $("#postopt").change( function() {
			$("#postopt").bind("click", function() {
				if (this.checked) {
					
					sendPost("yes");
					//$("#sendEmail").attr("checked", "checked");
				} else {
					//sendFax("no");
					sendPost("no");
					/*$("#sendEmail").attr("checked", false);
					$("#sendFax").attr("checked", false);
					$("#sendPost").attr("checked", false);*/
				}
			}); 
		function sendFax(a){
			switch (a) {
				case "yes":
					$("#formFax").addClass("validator-required");
					$('<span class="required">&nbsp;*</span>').appendTo("label[for='formFax']");
					break;
				case "no":
					$("#formFax").removeClass("validator-required");
					$("#formFax").removeClass("validation-failed");
					$("span", "label[for='formFax']").remove();
					$("#ValidationInlineErrorMessage-formFax").hide();
					break;
			}
		}
		function sendPost(a){
			switch (a) {
				case "yes":
					$("#formAddress").addClass("validator-required");
					$('<span class="required">&nbsp;*</span>').appendTo("label[for='formAddress']");
					$("#formPLZ").addClass("validator-required");
					$('<span class="required">&nbsp;*</span>').appendTo("label[for='formPLZ']");
					$("#formCity").addClass("validator-required");
					$('<span class="required">&nbsp;*</span>').appendTo("label[for='formCity']");
					break;
				case "no":
					$("#formAddress").removeClass("validator-required");
					$("#formAddress").removeClass("validation-failed");
					$("span", "label[for='formAddress']").remove();
					$("#ValidationInlineErrorMessage-formAddress").hide();
					$("#formPLZ").removeClass("validator-required");
					$("#formPLZ").removeClass("validation-failed");
					$("span", "label[for='formPLZ']").remove();
					$("#ValidationInlineErrorMessage-formPLZ").hide();
					$("#formCity").removeClass("validator-required");
					$("#formCity").removeClass("validation-failed");
					$("span", "label[for='formCity']").remove();
					$("#ValidationInlineErrorMessage-formCity").hide();
					break;
			}
	}
	},
	/**
	 * Initializes the apppartments template.
	 */
	templateAppartments: function() {		
		// table
		$(".appartments").tableHover();		
				
	},
	/**
	 * Initializes the apppartment template.
	 */
	templateAppartment: function() {
		// formular
		jQuery.validationAide.resetForm("#objectformular");
		jQuery("#formRefA", "#objectformular").click(function(){
			jQuery("#formRefCText", "#objectformular").val("");
			jQuery("#formRefDText", "#objectformular").val("");
			jQuery("#formRefEText", "#objectformular").val("");															 
		});
		jQuery("#formRefB", "#objectformular").click(function(){
			jQuery("#formRefCText", "#objectformular").val("");
			jQuery("#formRefDText", "#objectformular").val("");
			jQuery("#formRefEText", "#objectformular").val("");															 
		});
		jQuery("#formRefCText", "#objectformular").focus(function(){
			jQuery("#formRefC", "#objectformular").attr("checked", "checked");	
			jQuery("#formRefDText", "#objectformular").val("");
			jQuery("#formRefEText", "#objectformular").val("");															 
		});
		jQuery("#formRefDText", "#objectformular").focus(function(){
			jQuery("#formRefD", "#objectformular").attr("checked", "checked");
			jQuery("#formRefCText", "#objectformular").val("");
			jQuery("#formRefEText", "#objectformular").val("");
		});
		jQuery("#formRefEText", "#objectformular").focus(function(){
			jQuery("#formRefE", "#objectformular").attr("checked", "checked");	
			jQuery("#formRefCText", "#objectformular").val("");
			jQuery("#formRefDText", "#objectformular").val("");															 
		});
		jQuery("#objectformular").unbind("submit");
		jQuery("#objectformular").bind("submit",function(){
			var validated = jQuery.validationAide.validateForm("#objectformular");
			if (validated) {
				var queryString = jQuery(this).serialize();
			    var formAction = this.action + "?" + queryString;
			    jQuery(this).popupWindow(formAction,{ width:830,height:900,top:50,left:50,scrollbars: "yes" });
				return false;
			}
			else {
				return false;
			}	
		});
		jQuery("#formReset").bind("click", function(){
				jQuery.validationAide.resetForm("#objectformular");
		});
	},
	
	/**
	 * Initializes the application template.
	 */
	 templateApplication: function() {
	 	window.print();
	 },
	 
	 
	 /**
	 * Initializes the manage template.
	 */
	 templateManage: function() {
		
	 	// radio buttons
		jQuery(".manageButton").click(function() {
						
	        // values
			var id = jQuery(this).attr("id");
			var itemId = jQuery.trim(id.split("-")[1]);
			var itemValue = jQuery.trim(jQuery(this).attr("value"));
			var csvFile = Pensimo.CSVFILE;
			var r = Math.random();
			
			// status	
			//jQuery.log($(this).parent().parent());	
			//jQuery(this).removeClass("statusUnchanged");	
			//jQuery(this).removeClass("statusUpdated");
			//jQuery(this).addClass("statusChange");	
			// change
			jQuery.get("update.php", { csv:csvFile, entryId: itemId, entryValue: itemValue, rand:r }, function(data){
				if (data == "success") {
					// status
					//jQuery(this).removeClass("statusChange");
					//jQuery(this).addClass("statusUpdated");
				}
				else {
					// status
					//jQuery(this).removeClass("statusChange");
					//jQuery(this).addClass("statusError");
					alert("Server Error: Please contact your Administrator. " + data);
				}
  			});
		});
		
		// table
		$(".appartments").tableHover();	
	 },
	 
	 /**
	 * Initializes the gallery.
	 */
	 initializeGallery: function() {
		 // init
		 jQuery(".gallery ul.imagelist img").each(function(i){		
				// first
				if (i == 0) {
					$("#galleryImageLarge").attr("src", $(this).attr("src"));
					$("#galleryImageLarge").attr("alt", $(this).attr("alt"));
					jQuery(this).parent().addClass("first");
				}
				jQuery(this).attr("width",90);
				jQuery(this).attr("height",60);				
		 });
	 	// event
		jQuery(".gallery img").click(function() {
    			$("#galleryImageLarge").attr("src", $(this).attr("src"));
				$("#galleryImageLarge").attr("alt", $(this).attr("alt"));	
		});
	 },
	initializeSlideshow: function() {
		
		var flashvars = {};
		var params = {};
		var attributes = {};
		attributes.id = "regimo";
		swfobject.embedSWF("riedhof.swf", "myAlternativeContent", "802", "538", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
		
		jQuery("#slideshow").overlay({
			expose: { 
				color: '#ccc',
				loadSpeed: 200, 
				opacity: 0.6
			}
		});
		jQuery("#open_slideshow").bind("click", function(e){
			modalTrigger = jQuery("#slideshow").overlay().load();
			
			e.preventDefault();
			return false;
		});
	}
}

/**
 * Initialize on ready and load events.
 */
jQuery(document).ready(function(){
		Pensimo.initializeReady();
});
jQuery(window).load(function(){
		Pensimo.initializeLoad();
});
