var animSpeed = 200;
var fieldDefaultColour = '#666666';
var fieldRequiredColour = '#CC0000';

// document load
$JQ(document).ready(function() {
	CEVA.GENERIC.addMenuIframe();
	CEVA.GENERIC.bindMenu();
	CEVA.POPUPS.movePopupHTML();
	CEVA.POPUPS.bindPopupsLink();
	CEVA.FORM.prepopulateFieldsText();
	CEVA.FORM.bindFieldsRequired();
	CEVA.GENERIC.bindDocument();
});

// *******************************************************************************************

CEVA = {
};

// *******************************************************************************************

CEVA.GENERIC = {
	
	addMenuIframe: function() {
		$JQ('.navigationLeft ul ul').append('<!--[if lte IE 6.5]><iframe></iframe><![endif]-->');
		$JQ('.navigationLeft ul ul iframe').each(function() {
			var eachElement = $JQ(this);
			eachElement.parent().css('background-position','0% ' + LIBRARY.getTotalHeight(eachElement.parent()) + 'px')
		});
	},

	bindMenu: function() {
		$JQ('.navigationLeft ul.menu li').unbind('mouseenter');
		$JQ('.navigationLeft ul.menu li').bind('mouseenter',function() {
			var theElement = $JQ(this);
			$JQ('ul', theElement).css('left','0px');
			theElement.addClass('hovered');
		});

		$JQ('.navigationLeft ul.menu li').unbind('mouseleave');
		$JQ('.navigationLeft ul.menu li').bind('mouseleave',function() {
			var theElement = $JQ(this);
			$JQ('ul', theElement).css('left','-9999px');
			theElement.removeClass('hovered');
		});
	},

	bindDocument: function() {
		$JQ(document).unbind('mousedown');
		$JQ(document).bind('mousedown',function(click) {
			var theElement = $JQ(click.target);
			if(theElement.attr('id') != 'panelLogin' && !theElement.parents('#panelLogin').size()) {
				$JQ('#panelLogin').css('display','none');
			}
			if(theElement.attr('class') != 'popupBox' && !theElement.parents('.popupBox').size()) {
				$JQ('.popupBox').each(function() {
					var eachElement = $JQ(this);
					if (eachElement.css('display') == 'inline') {
					    if(this.id=="popupOtherSites")return false;//Added by Furqan on 05-Nov-2009
						CEVA.POPUPS.closePopup(eachElement);
						return false;
					}
				});
			}
		});
	}
};

// *******************************************************************************************

CEVA.FORM = {
	prepopulateFieldsText: function() {
		$JQ('input.fieldText').each(function() {
			var eachElement = $JQ(this);
			if (eachElement.attr('class').toLowerCase().indexOf('fieldprepopulate') >= 0) {
				var defaultFieldValue = CEVA.FORM.getDefaultFieldValue(eachElement)
				eachElement.val(defaultFieldValue);

				eachElement.unbind('focus');
				eachElement.bind('focus', function() {
					if (eachElement.val() == defaultFieldValue) {
						eachElement.val('');
					}
				});

				eachElement.unbind('blur');
				eachElement.bind('blur', function() {
					if (eachElement.val() == '') {
						eachElement.val(defaultFieldValue);
					}
				});
			}
		});
	},
	
	bindFieldsRequired: function() {
		$JQ('.fieldRequired').each(function() {
			var eachElement = $JQ(this);

			if(eachElement.attr('tagName').toLowerCase() == 'select') {
				eachElement.unbind('change');
				eachElement.bind('change', function() {
					if (eachElement.attr('selectedIndex') != 0) {
						eachElement.css('color', fieldDefaultColour);
					} else {
						eachElement.css('color', fieldRequiredColour);
					}
				});
			}

			if(eachElement.attr('tagName').toLowerCase() == 'input') {
				eachElement.unbind('change');
				eachElement.bind('change', function() {
					var defaultFieldValue = CEVA.FORM.getDefaultFieldValue(eachElement)
					if ((eachElement.val() != defaultFieldValue) && (eachElement.val() != '')) {
						eachElement.css('color', fieldDefaultColour);
					} else {
						eachElement.css('color', fieldRequiredColour);
					}
				});
			}
		});
	},
	
	getDefaultFieldValue: function(theElement) {
		var classNames = theElement.attr('class').split(' ');
		for (var i=0; i<classNames.length; i++) {
			if (classNames[i].toLowerCase().indexOf('fieldprepopulate') >= 0) {
				var defaultFieldValue = classNames[i].substring(16).replace('_', ' ').replace('DOTS', '...');
				break;
			}
		}
		return defaultFieldValue;
	}
};

// *******************************************************************************************

CEVA.POPUPS = {
	movePopupHTML: function() {
		$JQ('.popupBox').each(function() {
			var eachElement = $JQ(this);
			$JQ('.popupControl').each(function() {
				var eachLink = $JQ('a', this);
				if (eachLink.attr('href').substring(1) == eachElement.attr('id')) {
					LIBRARY.moveHTML(eachElement, eachLink.parent());
					return false;
				}
			});
		});
	},

	bindPopupsLink: function() {
		$JQ('.popupControl>a').unbind('click');
		$JQ('.popupControl>a').bind('click',function() {
			var theElement = $JQ(this);
			var theTarget = $JQ(theElement.attr('href'));
			CEVA.POPUPS.openPopup(theTarget);
			return false;
		});

		$JQ('.popupHeading img').unbind('click');
		$JQ('.popupHeading img').bind('click',function() {
			var theElement = $JQ(this);
			CEVA.POPUPS.closePopup(theElement.parents('.popupBox'));
			return false;
		});
	},
	
	openPopup: function(theElement) {
		theElement.css('display','inline');
		LIBRARY.setZindex(theElement, 1);
	},

	closePopup: function(theElement) {
		theElement.css('display','none');
		LIBRARY.setZindex(theElement, 0);
	}
};

// *******************************************************************************************

