/**
 * @author gaurakum
 */

var processing = false;
/* added images here for particular page */
var imageMap = {'home': 
					['./images/happycustomers.jpg'],
				'services': ['./images/movinglitesatnight.gif'],
				'technology': ['./images/macbook_tea.gif'],
				'domain': ['./images/laptopusing.jpg'],
				'aboutus': ['./images/ourpeople.jpg']
}

function fnClick(e) {	
	var srcElement = YAHOO.util.Event.getTarget(e);	
	
	if(srcElement.firstChild.href != null) {
		location = srcElement.firstChild.href;
		
	}	
}

function showPage(navKey, bookmark) {
	if (bookmark != null) {
		window.location = "?navKey=" + navKey + '#' + bookmark;
	} else {
		window.location = "?navKey=" + navKey;
	}
}

function highlightMenuItem(e) {
	var srcElement = YAHOO.util.Event.getTarget(e);
	// if the element is an Heading element
	if (srcElement.tagName.match(/h/i)) {
		// if the element is not selected
		if(!YAHOO.util.Dom.hasClass(srcElement, 'selected')) {
			YAHOO.util.Dom.replaceClass(srcElement, null, 'selected');	
		}
		
	} else if(srcElement.tagName.match(/a/i)) {
		// if the element is not selected
		if (!YAHOO.util.Dom.hasClass(srcElement.parentNode, 'selected')) {
			// if the element is an Anchor change the next heading eleemnt
			YAHOO.util.Dom.replaceClass(srcElement.parentNode, null, 'selected');
		}
	}
	 	
}

function resetMenuItem(e) {
	var srcElement = YAHOO.util.Event.getTarget(e);
	if (srcElement.tagName.match(/h/i)) {
		// if the element is not selected
		if(YAHOO.util.Dom.hasClass(srcElement, 'selected')) {
			YAHOO.util.Dom.removeClass(srcElement, 'selected');	
		}
		
	} else if(srcElement.tagName.match(/a/i)) {
		// if the element is not selected
		//if (!YAHOO.util.Dom.hasClass(srcElement.parentNode, 'selected')) {
			// if the element is an Anchor change the next heading eleemnt
			//alert(srcElement.parentNode.innerHTML);
			YAHOO.util.Dom.removeClass(srcElement.parentNode,  'selected');	
			//alert('called');
		//}
	}	
}

function findMenuLevel(elem) {
	var current = elem;
	while(current.tagName != 'BODY' || current.id == "mainnavigation") {
		if(current.className.match(/menuitem/)) {			
			return current.className;
		} else {
			current = current.parentNode;
		}
	}
}


function loadPage() {
	//alert('Hello World, page loaded!');
	YAHOO.util.Event.addListener("mainnavigation", "click", fnClick);  
	YAHOO.util.Event.addListener("mainnavigation", "mouseover", highlightMenuItem);
	YAHOO.util.Event.addListener("mainnavigation", "mouseout", resetMenuItem);
	YAHOO.util.Event.addListener("header", "click", takeAction);
	YAHOO.util.Event.addListener("copyrightdiv", "click", takeAction);
	YAHOO.util.Event.addListener("grayout", "click", clearGrayout);
	YAHOO.util.Event.addListener("maincontent", "click", takeAction);
	
	applyNodeStyle();
	//showContentByNavKey('ctxFrame','home');		
	
}

function displayRelevantImages() {
	var navKey = gup('navKey');
	if(navKey == null || navKey == "")
		navKey = "home";
	var images = imageMap[navKey];
	changeImages(images);
}

function changeImages(images){
	i = 0;
	if (images!=null && images.length > 0) {		
		var imgPath = images[i];
		setTimeout(function() {
			disappear('changing_div', 0.1);			
		}, 0);
		setTimeout(function() {				
			var changingDiv = YAHOO.util.Dom.get("changing_div");
			changingDiv.innerHTML = "<img src='" + imgPath + "' width='350px' height='200px' >";
		}, 500);
		setTimeout(function() {
			appear('changing_div', 1);			
		}, 1000);
		
		i++;
	} else return;
	
	if(images.length == 1)
		return;
	
	setTimeout(function() {
		setInterval(function(){		
			if(images!=null && images.length > 0 && i < images.length) {
				var imgPath = images[i];			
				// disappear the image in a new thread, in 2 sec
				setTimeout(function() {
					disappear('changing_div', 3);			
				}, 0);
				// change the image after 2.5 sec	
				
				setTimeout(function() {				
					var changingDiv = YAHOO.util.Dom.get("changing_div");
					changingDiv.innerHTML = "<img src='" + imgPath + "' width='350px' height='200px' >";
				}, 3500);
				// appear the image in 3 sec				
				setTimeout(function() {
					appear('changing_div', 2);			
				}, 4000);
				
				
				i++;			
			} else {
				i = 0;	
			}
		}, 7000);	
	}, 2000)
	
	
}


function appear(actionElement, time) {
	var attributes = { 
	   opacity: { 	from: 0.0,   	to: 1.0	   }
	};	
	var myAnim = new YAHOO.util.Anim(actionElement, attributes, time);
	myAnim.animate(); 
}

function disappear(actionElement, time) {
	var attributes = { 
	   opacity: { 	from: 1.0,   	to: 0	   }
	};	
	var myAnim = new YAHOO.util.Anim(actionElement, attributes, time);
	myAnim.animate();	
}


function showContentByNavKey(frame, navKey, params) {
	switch(navKey) {
		case 'home':
			YAHOO.util.Dom.get(frame).src = 'home.html';
			break;
	}
}

/* search for all nodes which have children and automatically show '+' sign */
function applyNodeStyle() {		
	var navKey = gup('navKey', window.location.href );
	
	for(var h=1; h<7; h++) {
		var nodes = YAHOO.util.Dom.getElementsByClassName('level' + h);
		for(var node in nodes) {
			var children = YAHOO.util.Dom.getChildren(nodes[node]);
			for (var child in children) {
				var currentNode = children[child];
				// either there will be anchors or divs inside the top level <div> element
				if (currentNode.tagName == 'DIV') {
					// if the current set of child nodes has a div, this means that the parent node has children
					var parentMenuItem = YAHOO.util.Dom.getPreviousSibling(currentNode);
					// parentMenuItem is an Anchor
					var hrefNavKey = gup('navKey', parentMenuItem.firstChild.getAttribute('href'));
					if(hrefNavKey != null) {					
						
						if (navKey == hrefNavKey) {
							YAHOO.util.Dom.setStyle(currentNode, 'display', 'block');
							YAHOO.util.Dom.setStyle(currentNode, 'background-color', 'white');
							YAHOO.util.Dom.setStyle(currentNode, 'color', 'black');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'color', 'black');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'background-color', 'white');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'border-right', '0');							
							if (parentMenuItem.innerText) { // for IE
								parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.innerText + '&nbsp;&nbsp;<img src="images/down-arrow-black4.png" style="top:0px; position:relative; border-width:0px" alt="collapse">';
							} else if(parentMenuItem.childNodes[0]){ // for firefox
								parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.textContent + '&nbsp;&nbsp;<img src="images/down-arrow-black4.png" style="top:0px; position:relative; border-width:0px" alt="collapse">';
							}
							continue;
						} else if(navKey == "" && hrefNavKey == 'home') {
							if (parentMenuItem.innerText) { // for IE
								parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.innerText + '&nbsp;&nbsp;<img src="images/down-arrow-black4.png" style="top:0px; position:relative; border-width:0px" alt="collapse">';
							} else if(parentMenuItem.childNodes[0]){ // for firefox
								parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.textContent + '&nbsp;&nbsp;<img src="images/down-arrow-black4.png" style="top:0px; position:relative; border-width:0px" alt="collapse">';
							}
							YAHOO.util.Dom.setStyle(currentNode, 'display', 'block');
							YAHOO.util.Dom.setStyle(currentNode, 'background-color', 'white');
							YAHOO.util.Dom.setStyle(currentNode, 'color', 'black');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'color', 'black');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'background-color', 'white');
							YAHOO.util.Dom.setStyle(parentMenuItem.parentNode, 'border-right', '0');							
						}
					}
					// change the style of the parent menu item
					//var img = document.createElement('img');
					//img.src = 'images/fwd-arrow-black4.png'
					//img.border = "0";
							
					if (parentMenuItem.innerText) { // for IE
						parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.innerText + '&nbsp;&nbsp;<img src="images/fwd-arrow-black4.png" style="top:0px; position:relative; border-width:0" alt="expand">';
					} else if(parentMenuItem.childNodes[0]){ // for firefox
						parentMenuItem.firstChild.innerHTML = parentMenuItem.firstChild.textContent + '&nbsp;&nbsp;<img src="images/fwd-arrow-black4.png" style="top:0px; position:reative; border-width:0" alt="expand">';
					}
					//parentMenuItem.style.marginLeft = '-5px';
					//					
				} else {
					// do nothing
				}
			}			
		}		
	}
}

function gup( name, urlString )
{
// alert(window.location.href)
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( urlString);
  if( results == null )
    return "";
  else
    return results[1];
} 

function takeAction(e) {
	if(processing)
		return;	
	
	var srcElement = YAHOO.util.Event.getTarget(e);
	
	var actionElementId = srcElement.id;
	if(srcElement.className == 'rfqlink') {
		actionElementId = 'rfqlink';
	}
	if(srcElement.id == "rfqlink" || srcElement.id == "disclaimerlink" || srcElement.className == 'rfqlink') {
		processing = true;
		var x = YAHOO.util.Dom.getX(srcElement);
		var y = YAHOO.util.Dom.getY(srcElement);
		switch(actionElementId) {
			case "sitemaplink": 
				var actionElement = YAHOO.util.Dom.get("sitemap");
				var ht = 100;
				var wt = 400;
				break;
			case "rfqlink":
				var actionElement = YAHOO.util.Dom.get("rfq");
				var ht = 430;
				var wt = 375;
				
				break;
			case "disclaimerlink":
				var actionElement = YAHOO.util.Dom.get("disclaimer");
				var ht = 500;
				var wt = 500;
				break;
			default:
				return;
		}
		
		
		if (YAHOO.util.Dom.getStyle(actionElement, 'position') != 'absolute' || actionElementId != 'disclaimerlink') {
			// not IE
			YAHOO.util.Dom.setStyle(actionElement, 'left', x + 'px');
			YAHOO.util.Dom.setStyle(actionElement, 'top', y + 'px');
		}
		//YAHOO.util.Dom.setStyle(actionElement, 'width', 0);
		//YAHOO.util.Dom.setStyle(actionElement, 'height', 0);
		YAHOO.util.Dom.setStyle(actionElement, 'opacity', '0');
		YAHOO.util.Dom.setStyle(actionElement, 'display', 'block');
		YAHOO.util.Dom.setStyle(actionElement, 'z-index', '3');		

		var clientHeight = YAHOO.util.Dom.getClientHeight();		
		var clientWidth = YAHOO.util.Dom.getClientWidth();		
		
		if (YAHOO.util.Dom.getStyle(actionElement, 'position') != 'absolute' || actionElementId != 'disclaimerlink') {
			// not disclaimer or non IE
			var leftPos = parseInt((clientWidth - wt)/2);
			var topPos = parseInt((clientHeight - ht) / 2);
			
			var attributes = { 
			   width: { to: wt }, 
			   height: { to: ht } ,
			   left: {to:leftPos },
			   top: {to:topPos},
			   opacity: {from:0, to: 1}		   
			}; 		 
		} else {
			// disclaimer for IE
			var attributes = { 
			   width: { to: wt }, 
			   height: { to: ht } ,
//			   left: {to:leftPos },
	//		   top: {to:topPos},
			   opacity: {from:0, to: 1}		   
			}; 		 
			
		}
		
		
		
		var bgattrib = { 		   
		   opacity: {from:0, to: 0.5},
		   backgroundColor: {to: '#000000'}
		}; 		 
		
		YAHOO.util.Dom.setStyle('grayout', 'backgroundColor', '#CCCCCC');
		YAHOO.util.Dom.setStyle('grayout', 'z-index', '2');
		YAHOO.util.Dom.setStyle('grayout', 'opacity', '0.5');
		YAHOO.util.Dom.setStyle('grayout','height', YAHOO.util.Dom.getDocumentHeight() + 'px');
		var myAnim2 = new YAHOO.util.Anim('grayout', bgattrib, 0.);
		var myAnim = new YAHOO.util.Anim(actionElement, attributes, 0.3); 	
		

		//myAnim2.animate();
		myAnim.animate(); 
		
		//var grayout = YAHOO.util.Dom.get('grayout');
		var h1 = YAHOO.util.Dom.getDocumentHeight();
		var h2 = YAHOO.util.Dom.getClientHeight();
		var h3 = YAHOO.util.Dom.getViewportHeight();
	}
}

function clearGrayout(e) {
	if (YAHOO.util.Dom.getStyle('rfq', 'display') == 'none') {
		YAHOO.util.Dom.setStyle('grayout', 'backgroundColor', '');
		YAHOO.util.Dom.setStyle('grayout', 'z-index', '-1');
		YAHOO.util.Dom.setStyle('sitemap', 'display', 'none');
		//YAHOO.util.Dom.setStyle('rfq', 'display', 'none');
		YAHOO.util.Dom.setStyle('disclaimer', 'display', 'none');
		processing = false;
	}
}

function validateForm(oForm)
 {
 		if(oForm.elements['customerName'].value.length<1)
 		{
 			alert("Providing your correct name helps us serve you better. Please fill your name in the Name field.");
 			return false;
 		}
 	
 		var regex_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 		if(oForm.elements['email'].value.length<1)
 		{
 			alert("Providing email helps us serve you better. Please fill in your email address.");
 			return false;
 		}
 		else if(!regex_email.test(oForm.elements['email'].value))
 		{
 			alert("Please correct the email address, it does not match the correct email address format");
 			return false;
 		}		
		
		oForm.submit();
 		return true;
 	
 }
 

function show(id) {
	//alert('hello')
	
	var htmlElement = YAHOO.util.Dom.get(id);
	//alert(htmlElement.innerHTML);
	if(htmlElement)
		YAHOO.util.Dom.setStyle(htmlElement, 'display', '');
	
}

function changeStyle(id, styleName, value) {
	YAHOO.util.Dom.setStyle(id, styleName, value);
}
