/**
 * Royal Canin exigent site scripts
 *
 * @version		0.1
 * @author		KMF - Werbung
 * @date			12.01.2010
 *
 *
 * Version history:
 * ================
 * Version 0.1 			Date: 012.01.2010 			Descr.: 	Home-Dummy
 */
 
 
// Create a closure to capsule  the hole site functionality
var rcexigent = (function()
{
	/** 
	 * class ModelsHolder()
	 * 
	 * Abstract root "models" class
	 *
	 * @author	S. Schnoor - KMF Werbung 
	 * @date		12.01.2010
	 *
	 * @return	object																					The ModelsHolder class
	 */
	function ModelsHolder() {};
	
	ModelsHolder.prototype = 
	{
		// Right anwser for "lottery page"
		int_lotteryAnswer: 1,
		
		ob_reportsFormFields: {
			page1: [
				{id: 'pet1_name', type: 'string', value: '.*'},
				{id: 'pet1_breed', type: 'select', value: '.{1,}'},
				{id: 'pet1_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
				{id: 'pet1_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
				{id: 'pet1_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'},
				{id: 'pet1_behavior', type: 'radio', name: 'pet1_cat_food_behavior', value: true},
				{id: 'pet1_foodtype', type: 'radio', name: 'pet1_cat_food_type', value: true},
				{id: 'pet1_bodytype', type: 'radio', name: 'pet1_cat_body_type', value: true}
			],
			
			page2: [
				{id: 'pet1_feed_bias', type: 'checkbox-list', name: 'pet1_cat_food_bias[]', value: true}
			],
			
			page3: [
				{id: 'salutation', type: 'select', value: '.{1,}'},
				{id: 'forename', type: 'string', value: '.*'},
				{id: 'lastname', type: 'string', value: '.*'},
				{id: 'street', type: 'string', value: '.*'},
				{id: 'houseno', type: 'string', value: '^[0-9a-zA-z ]{1,6}$'},
				{id: 'zip', type: 'string', value: '^[0-9]{5}$'},
				{id: 'place', type: 'string', value: '.*'},
				{id: 'email', type: 'email', value: '.*'},
				{
					type: 'condition',
					conditions:
					[
						{id: 'second_animal', type: 'checkbox', value: true},
						{id: 'pet2_type_cat', type: 'single-radio', value: true}
					],
					fields:
					[
						{id: 'pet2_cat_name', type: 'string', value: '.*'},
						{id: 'pet2_cat_breed', type: 'select', value: '.{1,}'},
						{id: 'pet2_cat_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_cat_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_cat_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'}
					]
				},
				{
					type: 'condition',
					conditions:
					[
						{id: 'second_animal', type: 'checkbox', value: true},
						{id: 'pet2_type_dog', type: 'single-radio', value: true}
					],
					fields:
					[
						{id: 'pet2_dog_name', type: 'string', value: '.*'},
						{id: 'pet2_dog_breed', type: 'select', value: '.{1,}'},
						{id: 'pet2_dog_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_dog_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_dog_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'}
					]
				}
			]
		},
		
		ob_yieldFormFields: {
			page1: [
				{id: 'salutation', type: 'select', value: '.{1,}'},
				{id: 'forename', type: 'string', value: '.*'},
				{id: 'lastname', type: 'string', value: '.*'},
				{id: 'street', type: 'string', value: '.*'},
				{id: 'houseno', type: 'string', value: '^[0-9a-zA-z ]{1,6}$'},
				{id: 'zip', type: 'string', value: '^[0-9]{5}$'},
				{id: 'place', type: 'string', value: '.*'},
				{id: 'email', type: 'email', value: '.*'},
				{id: 'pet1_name', type: 'string', value: '.*'},
				{id: 'pet1_breed', type: 'select', value: '.{1,}'},
				{id: 'pet1_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
				{id: 'pet1_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
				{id: 'pet1_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'},
				{
					type: 'condition',
					conditions:
					[
						{id: 'second_animal', type: 'checkbox', value: true},
						{id: 'pet2_type_cat', type: 'single-radio', value: true}
					],
					fields:
					[
						{id: 'pet2_cat_name', type: 'string', value: '.*'},
						{id: 'pet2_cat_breed', type: 'select', value: '.{1,}'},
						{id: 'pet2_cat_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_cat_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_cat_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'}
					]
				},
				{
					type: 'condition',
					conditions:
					[
						{id: 'second_animal', type: 'checkbox', value: true},
						{id: 'pet2_type_dog', type: 'single-radio', value: true}
					],
					fields:
					[
						{id: 'pet2_dog_name', type: 'string', value: '.*'},
						{id: 'pet2_dog_breed', type: 'select', value: '.{1,}'},
						{id: 'pet2_dog_birthday_dd', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_dog_birthday_mm', type: 'select', value: '[0-9]{1,2}'},
						{id: 'pet2_dog_birthday_yyyy', type: 'select', value: '[0-9]{2,4}'}
					]
				}
			]
		},
		
		
		/** 
		 * function HTMLLoader()
		 * 
		 * Load a external HTML and return the content
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		12.01.2010
		 *
		 * @params	str_pURL									string							The URL of the external page
		 * @params	obj_pCallback							object							Callback function which get the HTML context
		 */
		HTMLLoader: function(str_pURL, obj_pCallback)
		{
			var arr_url = str_pURL.split("?");
			if (! obj_pCallback) var obj_pCallback = function() {};
				
			jQuery.ajax(
			{
				cache: true,
				type: 'GET',
				dataType: 'html',
				url: arr_url[0],
				data: arr_url[1] ? arr_url[1] : '',
				
				success: function(str_html)
				{
					obj_pCallback(str_html);
				}
			});
		},
		
		
		/** 
		 * function Validate()
		 * 
		 * Validate a field
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		12.01.2010
		 *
		 * @params	str_pType									string							
		 * @params	obj_pValue								object
		 * @params	obj_pCondition						object
		 *
		 * @return	bool_valid								boolean
		 */
		Validate: function(str_pType, obj_pValue, obj_pCondition)
		{
			var bool_valid = false;
			 
			switch(str_pType)
			{
				case 'string':
					bool_valid = ((obj_pValue != "") && (obj_pValue.search(obj_pCondition) > -1));
					break;
					
				case 'email':
					bool_valid = ((obj_pValue != "") && (obj_pValue.indexOf("@") > -1) && (obj_pValue.search(obj_pCondition) > -1));
					break;
					
				case 'select':
					bool_valid = (obj_pValue.search(obj_pCondition) > -1);
					break;
					
				case 'radio':
				case 'single-radio':
				case 'checkbox':
				case 'checkbox-list':
					bool_valid = (obj_pValue == obj_pCondition);
					break;
			}
			
			return bool_valid;
		},
		
		ExistMail: function(str_pType, str_pMailAddr, obj_pCallback)
		{
			 Models.HTMLLoader("checkmail.php?type=" + str_pType + "&mail=" +str_pMailAddr, function(str_pReturnValue)
			 {
				 obj_pCallback((str_pReturnValue == 1) ? true : false);
			 });
		}
	}
	var Models = new ModelsHolder();
	
	
	/** 
	 * class Controllers()
	 * 
	 * Abstract root "controllers" class
	 *
	 * @author	S. Schnoor - KMF Werbung 
	 * @date		12.01.2010
	 *
	 * @return	object																					The controller class
	 */
	function Controllers() {}
	
	Controllers.prototype = 
	{
		/** 
		 * function openOverlayWindow()
		 * 
		 * Load a external page into overlay popup window
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		12.01.2010
		 *
		 * @params	str_pSite									string							The URL of the external page
		 */
		openOverlayWindow: function(str_pSite)
		{
			Models.HTMLLoader(str_pSite, function(str_pHTML)
			{
				Views.openOverlayWindow(str_pHTML);
			});
		},
		
		
		/** 
		 * function closeOverlayWindow()
		 * 
		 * Close a overlay popup window
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		12.01.2010
		 */
		closeOverlayWindow: function()
		{
			Views.closeOverlayWindow();
		},
		 
		 
		/** 
		 * function testLotteryAnswer()
		 * 
		 * Check if the given lottery anwser is corrent
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		12.01.2010
		 */
		testLotteryAnswer: function()
		{
			var bool_ok = false;
			 
			if (Views.getLotteryAnswerValue() == Models.int_lotteryAnswer)
			{ 
				bool_ok = true;
			}
			else Views.showLotteryWrongAnswer();
			 
			return bool_ok;
		},
		 
		 
		/** 
		 * function showLottery()
		 * 
		 * Reset lottery view 
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		18.01.2010
		 */
		showLottery: function()
		{
			Views.showLottery();
		},
		 
		 
		/** 
		 * function showReports()
		 * 
		 * Show / Handle FAQ page
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		24.02.2010
		 */
		showReports: function()
		{
			Views.handleReports();
		},
		
		
		/** 
		 * function showFAQ()
		 * 
		 * Show / Handle FAQ page
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		18.01.2010
		 */
		showFAQ: function()
		{
			Views.handleFAQ();
		},
		 
		
		/** 
		 * function showAnimalPage()
		 * 
		 * Show / Handle "Nahrungsakzeptanz" pages
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		28.01.2010
		 */
		showAnimalPage: function()
		{
			Views.handleAnimalPage();
		},
		
		
		/** 
		 * function handleBuublePopups()
		 * 
		 * Show / Handle FAQ page
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		18.01.2010
		 */
		handleBuublePopups: function()
		{
			Views.handleBuublePopups();
		},
		 
		 
		/** 
		 * function handleForms()
		 * 
		 * Show / Handle Formular pages
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		21.01.2010
		 */
		handleForms: function()
		{
			Views.showForm();
			
			jQuery("body.yieldFormular form, body.reportsForm form").submit(function(obj_pEvent, bool_pSkipValidation)
			{
				if (bool_pSkipValidation) return true;
				else
				{
					var arr_errors = rcexigent.checkForm(jQuery("body")[0].className, jQuery("form input[name*='send_form']").attr("name").substr(9));
					
					// Check if email adress is allready in use
					if (jQuery("body").hasClass("reportsForm")) var str_userType = "AdviceUser";  
					else if (jQuery("body").hasClass("yieldFormular")) var str_userType = "RaffleUser";
					
					if ((jQuery("form #email").length > 0) && (jQuery("form #email").val() != ""))
					{
						bool_mailExist = rcexigent.checkMail(str_userType, jQuery("form #email").val(), function(bool_pExist)
						{
							if (jQuery("body").hasClass("reportsForm")) var str_mailErrorText = "E-Mail: Sie haben bereits mit dieser E-Mail-Adresse ein Erfahrungsbericht eingereicht!";  
							else if (jQuery("body").hasClass("yieldFormular")) var str_mailErrorText = "E-Mail: Sie haben bereits mit dieser E-Mail-Adresse am Gewinnspiel teilgenommen!";
								
							if (bool_pExist) arr_errors.push(['email', str_mailErrorText]);
							
							if (arr_errors.length > 0)
							{
								Views.showFormErrorMessege(arr_errors);
							}
							else
							{
								jQuery("form").trigger("submit", true);
							}
						});
					}
					else
					{
						if (arr_errors.length > 0)
						{
							Views.showFormErrorMessege(arr_errors);
						}
						else
						{
							jQuery("form").trigger("submit", true);
						}
					}
					
					return false;
				}
			});
		},
		 
		
		/** 
		 * function getFormValue()
		 * 
		 * Get the value of a formular field
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		21.01.2010
		 *
		 * @params obj_pField									object							The field
		 *
		 * @return obj_value									object							
		 */
		getFormValue: function(obj_field)
		{
			var obj_value;
			
			switch (obj_field.type)
			{
				case 'string':
				case 'email':
					obj_value = jQuery("#" + obj_field.id).val();
					break;
					
				case 'select':
					obj_value = jQuery("#" + obj_field.id).val();
					break;
					
				case 'checkbox':
				case 'single-radio':
					obj_value =  (jQuery("#" + obj_field.id + ":checked").length > 0);
					break;
								
				case 'radio':
				case 'checkbox-list':
					obj_value = (jQuery("input[name='" + obj_field.name + "']:checked").length > 0);
					break;
					
				case 'condition':
					obj_value = obj_field.conditions;
					break;
			}
			
			return obj_value;
		},
		 
		
		/** 
		 * function checkForm()
		 * 
		 * Validate formular fields
		 *
		 * @author	S. Schnoor - KMF Werbung 
		 * @date		21.01.2010
		 *
		 * @params str_pFormular							string							Formular name
		 * @params int_pPage									integer							Formular page no.
		 */
		checkForm: function(str_pFormular, int_pPage)
		{
			var str_fieldsDefinitions;
			var arr_errors = [];
			
			if (str_pFormular.search(/reportsForm/) > -1) str_fieldsDefinitions = 'ob_reportsFormFields';
			else if (str_pFormular.search(/yieldFormular/)  > -1) str_fieldsDefinitions = 'ob_yieldFormFields';
				
			if (Models[str_fieldsDefinitions]["page" + int_pPage])
			{
				for (var i = 0; i < Models[str_fieldsDefinitions]["page" + int_pPage].length; i ++)
				{
					var bool_valid = false;
					var obj_field = Models[str_fieldsDefinitions]["page" + int_pPage][i];
					
					var obj_value = rcexigent.getFormValue(obj_field);
					
					if (obj_field.type != 'condition') 
					{
						bool_valid = Models.Validate(obj_field.type, obj_value, obj_field.value);
						
						if (! bool_valid) arr_errors.push(obj_field.id);
					}
					else
					{
						var bool_allConditions = true;
						
						for (x = 0; x < obj_value.length; x++)
						{
							var obj_conditionValue = rcexigent.getFormValue(obj_value[x]);
							
							bool_valid = Models.Validate(obj_value[x].type, obj_conditionValue, obj_value[x].value);
							
							bool_allConditions = bool_allConditions && bool_valid;
						}
						
						if (bool_allConditions)
						{
							for (x = 0; x < obj_field.fields.length; x++)
							{
								obj_conditionFieldValue = rcexigent.getFormValue(obj_field.fields[x]);
								bool_valid = Models.Validate(obj_field.fields[x].type, obj_conditionFieldValue, obj_field.fields[x].value);
								
								if (! bool_valid) arr_errors.push(obj_field.fields[x].id);
							}
						}
					}
				}
			} 
			
			return arr_errors;
		},
		
		checkMail: function(str_pType, str_pMailAddr, obj_pCallback)
		{
			Models.ExistMail(str_pType, str_pMailAddr, function(bool_pExist)
			{
				obj_pCallback(bool_pExist);
			});
		}
	}


	/** 
	 * class ViewsHolder()
	 * 
	 * Abstract root "views" class
	 *
	 * @author	S. Schnoor - KMF Werbung 
	 * @date		12.01.2010
	 *
	 * @return	object																	The ViewsHolder class
	 */
	function ViewsHolder() {}
	
	ViewsHolder.prototype =
	{
		openOverlayWindow: function(str_pHTML)
		{
			var str_template = '' +
				'<div id="overlayWindowBackground"></div>' + 
				'<div id="overlayWindow">' +
					'<div class="head">' +
						'<p class="close"><a href="#">Fenster schließen</a></p>' +
					'</div>' +
					'<div class="content"></div>' +
				'</div>';
				
			// Append window markup if necessary
			if (jQuery("#overlayWindow").length == 0) jQuery("body").append(str_template);
			
			// Set height of background
			jQuery("#overlayWindowBackground").css({
				height: jQuery("body").height()
			});
			
			// Fade in window background
			jQuery("#overlayWindowBackground").css({
					display: "block",
					opacity: 0
			});
			
			jQuery("#overlayWindowBackground").fadeTo(350, 0.8, function() 
			{
				var obj_overlay = jQuery("#overlayWindow");
				
				// Append HTML-Content
				obj_overlay.find("div.content").html(str_pHTML);
				
				if (window.isIE6)
				{
					// Set overlay window position
					// 1. Switch display to "block" so that ViewHelpers.setOverlayWindowPosition() can calculate the overlay windows dimensions
					obj_overlay.css({
						display: "block",
						top: "-10000px"
					});
					
					// 2. Position overlay window
					ViewHelpers.setOverlayWindowPosition();
					
					// 3. Hide overlay window again for fadeIn animation
					jQuery("#overlayWindow").css({
						display: "none"
					});
				}
				
				// Show window
				//jQuery("#overlayWindow").fadeTo(0);
				jQuery("#overlayWindow").fadeIn(500);
				
				// Register new event listener
				// 1. close window
				jQuery("#overlayWindow p.close a").bind("click", function() 
				{
					rcexigent.closeOverlayWindow();
				});
				
				jQuery("body").bind("keydown", function(obj_event) 
				{
					if (obj_event.which == 27) rcexigent.closeOverlayWindow();
				});
				
				// 2. Resize & scoll browser window
				jQuery(window).bind("resize", function()
				{
					if (window.isIE6) ViewHelpers.setOverlayWindowPosition();
				});
				
				jQuery(window).bind("scroll", function()
				{
					if (window.isIE6) ViewHelpers.setOverlayWindowPosition();
				});
			});
		},
		
		
		closeOverlayWindow: function(str_pHTML)
		{
			// Append window markup if necessary
			if (jQuery("#overlayWindow").length > 0)
			{
				// Fade out window and background
				jQuery("#overlayWindow").fadeOut(500, function()
				{
					jQuery("#overlayWindowBackground").fadeOut(350, function()
					{
						jQuery(this).find("div.content").empty();
					});
				});
				
				// Unbind registerd events
				jQuery("#overlayWindow p.close a").unbind("click");
				jQuery("body").unbind("keydown"); 
			}
		},
		
		
		showLottery: function()
		{
			jQuery("div.answers").show();
			jQuery("div.worngAnswer").hide();
		},
		
		
		getLotteryAnswerValue: function()
		{
			return jQuery("div.answers input[type='radio']:checked").val();
		},
		
		
		showLotteryWrongAnswer: function()
		{
			jQuery("div.answers").hide();
			jQuery("div.worngAnswer").show();
		},
		
		
		handleFAQ: function()
		{
			if (window.isIE6)
			{
				var int_fadeTime = 0;
				var int_slideTime = 0;
			}
			else
			{
				var int_fadeTime = 100;
				var int_slideTime = 350;
			}
			
			jQuery(".faq dt").click(function() 
			{
				// Close old entry
				if (jQuery(".faq dd.current")[0])
				{
					jQuery(".faq dd.current p.close").trigger('click');
				}
					
				// Show / Open new entry
				jQuery(this).slideUp(int_slideTime, function() 
				{
					jQuery(this).addClass("current");
				});
					
				jQuery(this).next("dd:first").slideDown(int_slideTime, function() 
				{
					jQuery(this).find("p.close").fadeIn(int_fadeTime);
					jQuery(this).addClass("current");
				});
			});
			
			// Handle click on close button
			jQuery(".faq dd p.close").click(function() 
			{
				jQuery(this).fadeOut(int_fadeTime, function()
				{
					// Close entry
					jQuery(this).parents('dd:first').slideUp(int_slideTime, function() 
					{
						jQuery(this).removeClass("current");
					});
						
					jQuery(this).parents('dd:first').prev("dt:first").slideDown(int_slideTime, function() 
					{
						jQuery(this).removeClass("current");
					});
				});
			});
		},
		
		handleAnimalPage: function()
		{
			if (! window.isIE6)
			{
				jQuery(window).scroll(function() 
				{
					var obj_container = jQuery("#content div.details");
					var obj_arrows = jQuery("#content div.details div.arrows");
					
					var int_minTop = obj_container.offset().top;
					var int_maxTop = obj_container.offset().top + obj_container.height() - obj_arrows.height() - 30;
					
					obj_arrows.css({
						top: "auto",
						left: "363px",
						position: "fixed"
					});
					
					if (obj_arrows.offset().top > int_maxTop)
					{
						obj_arrows.css({
							top: int_maxTop,
							left: '-2px',
							position: "absolute"
						});
					}
					else
					{
						obj_arrows.css({
							top: "auto",
							left: "363px",
							position: "fixed"
						});
					}
				});
			}
		},
		
		handleBuublePopups: function()
		{
			// 1. Cat description bubbles
			jQuery("div.paper div.content h3 strong").mouseover(function(obj_event) 
			{
				jQuery(this).jPopupBubble({
					int_top: obj_event.pageY,
					int_left: obj_event.pageX,
					str_title: jQuery(this).parent().text(),
					str_content: jQuery(this).parents("div.content").find("div.about").html(),
					bol_loadExternalContent: false,
					popupOpend: function()
					{
						jQuery("#jPopupBubble").css({
							top: jQuery("#jPopupBubble").position().top + jQuery("#jPopupBubble").height() + 25,
							left: jQuery("#jPopupBubble").position().left + Math.round(jQuery("#jPopupBubble").width() / 2) - 22
						});
					}
				});
			});
			
			// 2. Word explanation bubble
			jQuery("body.theAnimal div.intro p em").mouseover(function(obj_event) 
			{
				jQuery(this).jPopupBubble({
					int_top: obj_event.pageY,
					int_left: obj_event.pageX,
					str_title: "Was bedeutet " + jQuery(this).text() + "?",
					str_content: jQuery(this).next("span.textonly").find("span").text(),
					bol_loadExternalContent: false,
					popupOpend: function()
					{
						jQuery("#jPopupBubble").css({
							top: jQuery("#jPopupBubble").position().top + jQuery("#jPopupBubble").height() + 25,
							left: jQuery("#jPopupBubble").position().left + Math.round(jQuery("#jPopupBubble").width() / 2) - 22
						});
					}
				});
			});
			
			jQuery("div.paper div.content h3 strong, body.theAnimal div.intro p em").mouseleave(function()
			{
				jQuery("#jPopupBubble .jPopupBubble_title_bar_close").trigger("click");
			});
			
			// 3. Page switcher hint bubble
			jQuery("body.products div.pageSwitcher a").mouseover(function(obj_event) 
			{
				jQuery(this).jPopupBubble({
					int_top: obj_event.pageY,
					int_left: obj_event.pageX,
					str_title: "",
					str_content: jQuery(this).text(),
					bol_loadExternalContent: false,
					popupOpend: function()
					{
						jQuery("#jPopupBubble").css({
							top: jQuery("#jPopupBubble").position().top + jQuery("#jPopupBubble").height() + 25,
							left: jQuery("#jPopupBubble").position().left 
						});
					}
				});
			});
			
			jQuery("body.products div.pageSwitcher a").mouseleave(function(obj_event) 
			{
				jQuery("#jPopupBubble .jPopupBubble_title_bar_close").trigger("click");
			});
		},
		
		showForm: function()
		{
			// Show / hide "second animal radio boxes"
			jQuery("form #second_animal").click(function()
			{
				if (jQuery(this).attr("checked")) 
				{
					jQuery("#second_animal_radios").show();
					if (jQuery("#pet2_type_dog:checked").length > 0) jQuery("#second_animal_fields_dog").show();
					if (jQuery("#pet2_type_cat:checked").length > 0) jQuery("#second_animal_fields_cat").show();
				}
				else 
				{
					jQuery("#second_animal_radios").hide();
					jQuery("#second_animal_fields_dog").hide();
					jQuery("#second_animal_fields_cat").hide();
				}
			});
			
			// Show / hide "second animal"
			jQuery("form #second_animal_radios input[type='radio']").click(function()
			{
				if (jQuery(this).attr("id") == "pet2_type_cat")
				{
					jQuery("#second_animal_fields_cat").show();
					jQuery("#second_animal_fields_dog").hide();
				}
				else
				{
					jQuery("#second_animal_fields_dog").show();
					jQuery("#second_animal_fields_cat").hide();
				}
			});

			jQuery("form #email").change(function()
			{
				// Delete old error message
				jQuery("form fieldset.sendOptions p span.mailerror").remove();
				
				if (jQuery("form #newsletter_subscription").attr("checked"))
				{
					bool_mailExist = rcexigent.checkMail("NewsletterUser", jQuery(this).val(), function(bool_pExist)
					{
						if (bool_pExist) jQuery("form #newsletter_subscription").parent().prepend('<span class="mailerror">Sie haben sich bereits mit Ihrer E-Mail Adresse »' + jQuery("form #email").val() + '« für den Royal Canin Newsletter registriert!</span>');
					});
				}
			});
			
			
			jQuery("form #newsletter_subscription").click(function()
			{
				var obj_parent = jQuery(this).parent();
				
				if (jQuery(this).attr("checked"))
				{
					bool_mailExist = rcexigent.checkMail("NewsletterUser", jQuery("form #email").val(), function(bool_pExist)
					{
						if (bool_pExist) obj_parent.prepend('<span class="mailerror">Sie haben sich bereits mit Ihrer E-Mail Adresse »' + jQuery("form #email").val() + '« für den Royal Canin Newsletter registriert!</span>');
					});
				}
				else
				{
					// Delete old error message
					jQuery("form fieldset.sendOptions p span.mailerror").remove();
				}
			});
		},
		
		showFormErrorMessege: function(arr_pErrors)
		{
			var str_template = '<div class="error">' +
				'<h4>Bitte ergänzen Sie Ihre Angaben</h4>' + 
				'<p>Folgende Felder wurden nicht korrekt ausgefüllt.</p>' +
				'<ul>$1</ul></div>';
			
			// Remove old error message & highlights
			jQuery("form > div.error").remove();
			jQuery("form .error").removeClass("error");
			if (window.isIE6)
			{
				jQuery("form .minerror").removeClass("minerror");
				jQuery("form .seconderror").removeClass("seconderror");
			}
			
			// Build new error message string 
			var str_errors = '';
			for (i = 0; i < arr_pErrors.length; i++)
			{
				if (typeof(arr_pErrors[i]) == "string")
				{
					str_errors += '<li><a href="#' + arr_pErrors[i] + '">' + (jQuery("label[for=" + arr_pErrors[i] + "]").text() || jQuery("#" + arr_pErrors[i]).text()) + '</a></li>';
					str_errors = str_errors.replace(/\*/, '');
					
					jQuery("#" + arr_pErrors[i]).addClass("error");
					if (window.isIE6)
					{
						if (jQuery("label[for='" + arr_pErrors[i] + "']").hasClass("min")) jQuery("label[for='" + arr_pErrors[i] + "']").addClass("minerror");
						if (jQuery("label[for='" + arr_pErrors[i] + "']").hasClass("second")) jQuery("label[for='" + arr_pErrors[i] + "']").addClass("seconderror");
					}
						
					jQuery("label[for='" + arr_pErrors[i] + "']").addClass("error");
				}
				else
				{
					str_errors += '<li><a href="#' + arr_pErrors[i][0] + '">' + arr_pErrors[i][1] + '</a></li>';
					str_errors = str_errors.replace(/\*/, '');
					
					jQuery("#" + arr_pErrors[i][0]).addClass("error");
					if (window.isIE6)
					{
						if (jQuery("label[for='" + arr_pErrors[i] + "']").hasClass("min")) jQuery("label[for='" + arr_pErrors[i] + "']").addClass("minerror");
						if (jQuery("label[for='" + arr_pErrors[i] + "']").hasClass("second")) jQuery("label[for='" + arr_pErrors[i] + "']").addClass("seconderror");
					}
					
					jQuery("label[for='" + arr_pErrors[i][0] + "']").addClass("error");
				}
			}
			jQuery("form input[type='submit']").blur();
			
			jQuery("form").prepend(str_template.replace(/\$1/, str_errors));
			jQuery(window).scrollTop(jQuery("form div.error").offset().top);
			
			if (window.isIE6)
			{
				jQuery("#footer").css("display", "none").css("display", "block");
			}
			
			jQuery("form div.error a").click(function()
			{
				if (jQuery("#" + jQuery(this).attr("href").substr(1)).is("input, select, textarea")) 
				{
					jQuery("#" + jQuery(this).attr("href").substr(1))[0].focus();
					jQuery(window).scrollTop(jQuery("#" + jQuery(this).attr("href").substr(1)).offset().top);
					
					return false;
				}
			});
		},
		
		handleReports: function() 
		{
			jQuery("#content div.customervoices > div.page").each(function(int_pIndex) 
			{
				if (int_pIndex > 0) jQuery(this).hide();
			});
			
			var obj_more = jQuery("#content div.customervoices > p.readMore");
			obj_more.data("page", 0);
			
			jQuery("#content div.customervoices > p.readMore").click(function()
			{
				var int_page = parseInt(obj_more.data("page")) + 1;
				
				obj_more.data("page", int_page);
				jQuery("#content div.customervoices > div.page:eq(" + int_page + ")").fadeIn(500);
				
				if (int_page === jQuery("#content div.customervoices > div.page").length - 1) jQuery(this).hide(600);
				
				return false;
			});
		}
	}
	var Views = new ViewsHolder();
	
	/** 
	 * class ViewHelpersHolder()
	 * 
	 * Abstract root "viewhelper" class
	 *
	 * @author	S. Schnoor - KMF Werbung 
	 * @date		12.01.2010
	 *
	 * @return	object																	The ViewsHelpersHolder class
	 */
	function ViewHelpersHolder() {}

	ViewHelpersHolder.prototype =
	{
		setOverlayWindowPosition: function()
		{			
			var obj_overlay = jQuery("#overlayWindow");
			
			var int_top = (jQuery(window).scrollTop() + Math.round(jQuery(window).height() / 2)) - Math.round(obj_overlay.outerHeight() / 2);
			var int_left = (jQuery(window).scrollLeft() + Math.round(jQuery(window).width() / 2)) - Math.round(obj_overlay.outerWidth() / 2);
			
			obj_overlay.css({
				position: "absolute",
				top: int_top,
				left: int_left,
				margin: 0
			});
		}
	}
	var ViewHelpers = new ViewHelpersHolder();
	
	
	/**
	 * Return all controller as public objects
	 */
	return new Controllers();
})();



/**
 * Bind all events with controller actions
 */
jQuery(document).ready(function()
{
	// Add JSEnabled Class to body element
	jQuery("body").addClass("jsEnabled");
	
	// Click on footer links
	jQuery("#footer a").click(function() 
	{ 
		rcexigent.openOverlayWindow(jQuery(this).attr("href"));
		
		return false;
	});
	
	// Click on lottery answer button
	jQuery("body.yield div.answers input[type='submit']").click(function()
	{
		return rcexigent.testLotteryAnswer();
	});
	
	// Click on lottery "new try" button
	jQuery("body.yield div.worngAnswer a").click(function()
	{
		rcexigent.showLottery();
		
		return false;
	});
	
	// Handle FAQ-Page
	if (jQuery("body.reports").length > 0) rcexigent.showReports();
	
	// Handle FAQ-Page
	if (jQuery("body.faq").length > 0) rcexigent.showFAQ();
	
	// Handle "Nahrungsakzeptanz"-Pages
	if (jQuery("body.theAnimal").length > 0) rcexigent.showAnimalPage();
	
	// Handle bubble popups
	if (jQuery("body.theAnimal, div.paper div.content h3 strong, body.products div.pageSwitcher a").length > 0) rcexigent.handleBuublePopups();
	
	// Handle formulars
	if (jQuery("form").length > 0) rcexigent.handleForms();
});

	
