//Initiating jQuery to work with Prototype;
//var $j = jQuery.noConflict(); located inside jquery-1.2.3.min.js file

$j(document).ready(function() { onDocumentLoad(); });
$j(window).load(function() { onWindowLoad(); });

//Global Variables

/*
 * Standard page loading functions for prototype / jQuery
 */
function onDocumentLoad(){
	$j('#footerMenu ul').children('li').not(':last').append('<li>|</li>');

	$j("#twitterWrap").appendTo("a[title=Twitter]");

	var twitHeight = $j('#twitterWrap').height()+20;

	$j('#twitterWrap').css('margin-top','-'+twitHeight+'px');

	$j("a[title=Twitter]").hover(function(){
		$j("#twitterWrap").show()
	},function(){
		$j("#twitterWrap").hide();
	});
}

//Stuff put into this function doesnt run until the entire document is loaded flash and all
function onWindowLoad(){

	if ($j.browser.msie && $j.browser.version < 7) {
		intHeight = 350;
	} else {
		intHeight = 340;
	}

	windowWidth = $j(window).width();
	windowHeight = $j(window).height();

	intContactWidth = Math.floor(windowWidth / 2);
	intContactHeight = Math.min(Math.floor(windowHeight - 80), intHeight);

	intContactLeft = Math.floor((intContactWidth / 3)*-1);
	intContactTop = Math.floor((intContactHeight / 2)*-1);
	
	$j('DIV#headerContentLeft').click(function() { window.location = "http://www.lycosidea.com/"; });

	$j('.external').click(function(e){
		e.preventDefault();
		window.open(this.href);
	});

	$j('.contact, .contract').click(function(e){
	    e.preventDefault();

		windowWidth = $j(window).width();
		windowHeight = $j(window).height();

		intContactWidth = Math.floor(windowWidth / 2);
		intContactHeight = Math.floor(windowHeight - 80);
		intContactHeight = Math.min(Math.floor(windowHeight - 80), intHeight);

		intContactLeft = Math.floor((intContactWidth / 2)*-1);
		intContactTop = Math.floor((intContactHeight / 2)*-1);

		if($j(this).hasClass('contract')){
			$j('#subject').val('Contract request');
		}

		inputArray = {
			name:"Full Name",
			email:"email@email.com",
			vericode:"insert code above",
			subject:$j('#subject').val()
		};

	    $j('#contactForm').modal({
	    	overlayId: ('contactOverlay'),
	    	containerId: ('contactContainer'),
	    	onOpen: contactForm.open,
	    	onShow: contactForm.show
	    	//onclose doesnt work with the dual modal windows the closing is called else where
	    });

		$j("input").each(function(){
			var id = $j(this).attr("id");
			if($j(this).val()==inputArray[id]){
				$j(this).css("color","#999999");
			}
			if(!$j(this).val()){
				$j(this).css("color","#999999").val(inputArray[id]);
			}
		});

		$j("input").focus(function(e){
			var id = $j(this).attr("id");
			if($j(this).val()==inputArray[id]){
				$j(this).animate({ color: "#FFFFFF" }, "slow", function(e){
					$j(this).css('color','#000000').val('');
				});
			}
		}).blur(function(e){
			var id = $j(this).attr("id");
			if(!$j(this).val()||$j(this).val()==inputArray[id]){
				$j(this).css("color","#999999").val(inputArray[id]);
			}
		});

		$j("input[name='reset']").click(function(e){
			$j("input:not(:submit,:button,:hidden)").each(function(){
				$j(this).val("");
				$j(this).trigger('blur');
			});

			$j("#comments").val("");

			if ($j('#contactContainer .contactMessage:visible').length > 0) {
				var msg = $j('#contactContainer .contactMessage div');
				msg.fadeOut(400, function () {
					msg.empty();
					$j('#contactContainer .contactMessage').fadeOut(200);
					$j('#contactContainer').animate({
						height: intContactHeight
					});
				});
			}

			$j('#subject').val(inputArray["subject"]);

		});

		$j("#reloadCaptcha").click(function(){
			$j('#contactForm').children('form').find('img').replaceWith('<img style="border: 1px solid ;" alt="verification code" src="/manager/includes/veriword.php?rand='+Math.random()+'"/>');
		});
	});

	var contactForm = {
		message: null,
		open: function(dialog){
			$j('#contactContainer').css({'width': 400, 'height': 100, 'margin-left': -200, 'margin-top': intContactTop});
			
			if($j("iframe").css('display')){
				var currentID = $j(".cfMiddle").attr('id').replace("image","");
				var currentDoc = $j(coverFlowItems).get(currentID);
				var loc;
				
				siteURL = currentDoc.weburl+"/assets/plugins/lycosideaPortal/clientJS/"+clientIP+".js";
				$j.ajax({
					type: "GET",
					dataType: "script",
					url: siteURL,
					success: function(){
						loc = (typeof(loc) == 'undefined')?$j("#viewWindowFront").attr("src"):loc;
					}
				});
			}

			title = $j('#contactContainer .contactTitle').html();
			$j('#contactContainer .contactTitle').html('Loading...');
			dialog.overlay.fadeIn(1000, function () {
				dialog.container.fadeIn(1000, function () {
					dialog.data.fadeIn(500, function () {
						$j('#contactContainer').animate({
							height: intContactHeight
						}, function () {
							$j("#reloadCaptcha").trigger('click');
							title = ($j('#subject').val()==="Support request")?"Support Form":title;
							inputArray['subject'] = $j('#subject').val();
							$j('#contactContainer .contactTitle').fadeOut(200,function(){
								$j(this).html(title).fadeIn(200);
							});
							$j('#contactContainer form').fadeIn(1000);
							$j('#vericode').val(inputArray['vericode']);
							$j("#contactContainer .modalCloseImg").unbind().click(function(){
								contactForm.close(dialog);
							});
						});
					});
				});
			});

			$j("input[name='close']").click(function(){
				contactForm.close(dialog);
			});			

		},
		show: function (dialog) {
			$j("#contactContainer input[name='submit']").click(function (e) {
				e.preventDefault();
		
				var getContactContainerPreHeight = $j('#contactContainer').height();
				$j("input[name='topic']").val($j('#subject')[0].selectedIndex+1);
		
				// validate form
				if (contactForm.validate(dialog)) {
					$j('#contactContainer .contactMessage').fadeOut(function () {
						$j('#contactContainer .contactMessage').removeClass('contactError').empty();
					});
					$j('#contactContainer .contactTitle').html('Sending...');
					$j('#contactContainer form').fadeOut(200);
					$j('#contactContainer').animate({
						height: '200px'
					}, function () {
						$j('#contactContainer .contactLoading').fadeIn(200, function () {
							loc = (typeof(loc) == 'undefined')?"":loc;
							$j.ajax({
								url: $j('#contactContainer form').attr("action"),
								data: $j('#contactContainer form').serialize() + '&action=send&currentLoc='+loc+'&currentIP='+clientIP,
								type: 'post',
								cache: false,
								dataType: 'html',
								complete: function (xhr) {
									$j('#contactContainer .contactLoading').fadeOut(200, function () {
										var contactMessage = ($j(xhr.responseText).children('p').length > 0)?$j(xhr.responseText).children('p').parents("#contactForm").html():$j(xhr.responseText).children('.contactMessage').html();
										var veriCode = $j(xhr.responseText).children('form').find('img');
		
										if(contactMessage.indexOf("errors")!=-1){
											$j('#contactContainer').animate({
												height: getContactContainerPreHeight
											}, function(){
												$j('#contactContainer .contactTitle').html(title);
												$j('#contactForm').children('form').find('img').replaceWith(veriCode)
												$j('#contactContainer form').fadeIn(200);
												contactForm.message = contactMessage;
												contactForm.createError();
											});
										} else {
											$j('#contactContainer .contactTitle').html('Thank you!');
											$j("#contactContainer .contactMessage").html(contactMessage).fadeIn(200);
										}

										$j(".contactClose").click(function(){
											contactForm.close(dialog);
										})
									});
								},
								error: contactForm.error
							});
						});
					});
				} else {
					contactForm.createError();
				}
			});
		},
		close: function(dialog){
			$j('#contactContainer .contactMessage').fadeOut();
			$j('#contactContainer .contactTitle').html('Closing...');
			$j('#contactContainer form').fadeOut(500);
			$j('#contactContainer').animate({
				height: 100
			}, function () {
				dialog.container.fadeOut(500, function () {
					dialog.overlay.fadeOut(500, function () {
						$j.modal.close();
					});
				});
			});
		},
		validate: function(dialog){
			contactForm.message = '';
			$j('#contactContainer form input').each(function(){
				inputObj = $j(this);
				inputObjName = inputObj.attr('name');
				if(!inputObj.val() || inputObj.val()===inputArray[inputObjName]){
					contactForm.message = (contactForm.message.length > 0)?contactForm.message:'The following fields are required:<br />';
					inputObjName = inputObjName.charAt(0).toUpperCase() + inputObjName.slice(1);
					contactForm.message += inputObjName+' is required. ';
				}
			});

			if (contactForm.message.length > 0) {
				return false;
			} else {
				return true;
			}
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		createError: function() {
			if ($j('#contactContainer .contactMessage:visible').length > 0) {
				var msg = $j('#contactContainer .contactMessage div');
				msg.fadeOut(400, function () {
					msg.empty();
					contactForm.showError();
					msg.fadeIn(400);
				});
			} else {
				setHeight = ($j(contactForm.message).html().length>150)?40:30;
				$j('#contactContainer .contactMessage').animate({
					height: setHeight
				}, contactForm.showError);
			}
		},
		showError: function () {
			$j('#vericode').val('');
			$j('#contactContainer .contactMessage').html($j('<div class="contactError">').append(contactForm.message)).fadeIn(400,function(){
				$j('#contactContainer').animate({
					height: intContactHeight+setHeight
				});
			});
		}
	}
}