


$(document).ready(function(){
	
	//alert("-");
	$("#panel2, #panel3").click(function(){
	 	//alert("run script now"); 
	 })
	
	if($.browser){
		//alert("support")
		//$("body").append('<link rel="stylesheet" type="text/css" media="screen"  href="html/include/css/jsSupport.css" />')
	}
	
	
	
	
	innerPageName = ""  //global varibale to hold href of inner page
	var linkToLoad = "default.html"; //the href of page to load, default home page or error page?
	var errHref = "../error/404.html"
	var moveToId = "home";
	var moveToX = 0;
	var moveToY = 0; 
	var speed = 800; //speed of moving to panel
	var movedToIds = new Array("panel1"); //will hold array of panel ids already loaded so no new ajax calls, default the first panel
	var openedInnerPages = new Array(""); //will hold array of inner pages already loaded so no new ajax calls, default ?
	var ptn = new Array("0", "0"); //position array of panel - ptn(left, top), default 0,0
	//ptn[0] = 0;  //left  
	//ptn[1] = 0; //top
	
	
	//default settings
	$("#panelCont").scrollTo(
		{top:ptn[1], left:ptn[0]}, 
		speed,
		{onAfter:function(){
			
		} 
	});
				 
	 
	//begin mainNav click
	
	$(".mainNav a, .homeFeat div.oneItm a").click(function(){
		
		//alert($(this).attr("href"))
		//alert($(this).parents("div").attr("id"))
		//alert($(this).parent().hasClass("oneItm"));
		//$(this).parent("div.oneItm")
		
		//return false
		
		
		
		linkToLoad = $(this).attr("href");
		
		//var kk = 
		
		
		/*if(  $(this).parent().hasClass("oneItm") ){
			alert("feat");
		}else{
			alert("main");
		}*/
		$(".mainNav a").removeClass("active");
		
		if($(this).attr("id")!=""){
			moveToId = $(this).attr("id").replace("_","");
			//$(".mainNav a").removeClass("active");
			$(this).addClass("active")
		}else {
			
			
			
			if($(this).parents("div").attr("id")==""){
				window.open(linkToLoad, "_blank")
				return false;
			}else{
				
				moveToId = $(this).parents("div").attr("id").replace("dl","");
				$("#_"+moveToId).addClass("active").attr("href");
			}
			
			//$(".mainNav a").removeClass("active");
			
		}
		
	//	alert(moveToId)
		
		//return false
		
		//moveToId = $(this).attr("id").replace("_","");
		//moveToId = $(this).attr("id").replace("ctl00_mainNav1_ctl00_ctl00_", "");
		
		//$(".mainNav a").removeClass("active");
		//$(this).addClass("active")
		
		
		try{
			//get predefined positon of panel
			switch (moveToId) {
				case "panel1":
					ptn[0] = 0;  
					ptn[1] = 0; 
					break
				case "panel2":
					ptn[0] = 1150;  
					ptn[1] = 0; 
					break
				case "panel3":
					ptn[0] = 2300;  
					ptn[1] = 0; 
					break
				case "panel4":
					ptn[0] = 0;  
					ptn[1] = 855; 
					break
				case "panel5":
					ptn[0] = 1150;  
					ptn[1] = 855; 
					break
				case "panel6":
					ptn[0] = 2300;  
					ptn[1] = 855; 
					break
				case "panel7":
					ptn[0] = 0;  
					ptn[1] = 1700; 
					break
				case "panel8":
					ptn[0] = 1150;  
					ptn[1] = 1700; 
					break
				case "panel9":
					ptn[0] = 2300;  
					ptn[1] = 1700; 
					break
				case "panel10":
					ptn[0] = 0;  
					ptn[1] = 2450; 
					break	
					
				default: 
					ptn[0] = 0;  
					ptn[1] = 0; 
			};
			
			//return false;
			//call scroll to and then make ajax call on end
			$("#panelCont").scrollTo(
				{top:ptn[1], left:ptn[0]}, 
				speed,
				{onAfter:function(){
					//alert(moveToId)
					//return false;
					if($.inArray(moveToId, movedToIds)==-1){  //check to see if panel have been loaded before by check for id in array
						//console.log("not loaded so call ajax! before num in array =" + movedToIds.length);
						if(moveToId!=null || moveToId==""){
							movedToIds.push(moveToId);
							callAjaxfor(moveToId,linkToLoad,5000,errHref); //callAjaxfor(id,href,timeout)
							//$("body").append('<link rel="stylesheet" type="text/css" media="screen"  href="html/include/css/jsSupport.css" />')
						}else {
							window.location.href = errHref;
						}
						
						//console.log("not loaded so call ajax! after num in array =" + movedToIds.length);
					}else{
						//console.log("already, do nothing! num in array = "+ movedToIds.length);
					}
				} 
			});
			return false;

		}catch(err){
			//console.log("this error occurred: "+ err.decription);
			//window.location.href = errHref;
			//alert("herhe");
		}
		//move to panel, load content, replace title tag
		//maybe include history for bookmarking, back, forward
		
	});
	//end mainNav click
	
	
	
});


function callAjaxfor(id,href,timeout,errHref){ //id of panell, href
	
	//var retVal=""
	$.ajax({
		   //type:"get",
		//async: false,	   
		url: href,
		cache: false,
		processData: true,
		dataType: "html",
		cache: false,
		timeout: timeout,
		async: true,
		complete: function(){
			//alert("do something");
			//alert("back")
			callPageFunctions(id)
		
			//retVal = "ok";
			
		},
		error: function(jqXHR, textStatus, errorThrown){
			//window.location.href = errHref; //redirect to page on error
			//console.log("this error occurred: "+ errorThrown);
		},
		success: function(response){
			//movedToIds.push(id);
			//alert(response);
			try{
				var html = $('<div>').append(response).find("#" + id + " .panelBody").remove().html();
				
				//alert(id);
				if(html!=null){
					$("#" + id + " .panelBody").html("");
					$("#" + id + " .panelBody").html(html);
				}else {
					//window.location.href = errHref;
					//console.log(html);
				}
				
			}catch(err){
				//console.log("this error occurred: "+ err.decription);
				//window.location.href = errHref; //redirect to page on error
			}
		}
	});
	
	
	
	
}




function callPageFunctions(id){
	//alert(id + "cs = " +cs)
	switch (id) {
		case "panel1":
			
			break
		case "panel2":
			//web solutions
			//alert("df")
			$(".subNav li a:first").addClass("active");
			
			$(".subNav li a").click(function(){
				var x = $(this);
				var strSolutionsFunction = "";
				//make ajax call for page
				requestPage($(this).attr("href"), ".webSolutionsContent", 5000, "errHref", strSolutionsFunction);
				//show spinner in div
				$(".webSolutionsContent").html('<div class="spinner"><img src="../html/images/general/slideshowloader.gif" alt="loading" title="loading" /></div>');
				
				$(".subNav li a").removeClass("active");
				$(x).addClass("active");
				return false;
			})
			
			//careers link click
			$(".requestQuote a").click(function(){
				popUp($(this).attr("href"));
				return false;
			})
			//make accordion
			/*$(".webSolutions .pane:first").css("display", "block")
			
			$("#accordion").tabs("#accordion div", {
				tabs: 'img.solutionsHd', 
				effect: 'horizontal',
				onClick: function(x, y){
					//alert(y)
					//setTimeout($("#accordion img").not(".current").css("opacity", "0.5"), 2000)
					//$(y).css("opacity", "0.5");
					return false;
				}
			});*/
			//alert(id);
            break

		case "panel3":
			//this is internet marketing
			
			//initial opacity for the sub nav items
			$(".subNav li a").not(".active").find("img").css("opacity","0.5");
			
			//sub nav hover effects
			$(".subNav li a").hover(
				function(){
					if($(this).attr("class") ==""){
						$(this).find("img").stop().animate({opacity: 1.0}, "fast");
					}
				}, 
				function(){
					if($(this).attr("class") == ""){
						$(this).find("img").stop().animate({opacity: 0.5}, "fast");
					}
				}
			);
			
			var linkToLoad = "";
			var strNetMarketFunction = ""
			strNetMarketFunction = '$(".caseStudiesList a").click('
			strNetMarketFunction += '	function(){'
			strNetMarketFunction += '		var linkToLoadInner = $(this).attr("href");'
			strNetMarketFunction += '		$("#panelCont").scrollTo('
			strNetMarketFunction += '			{top:855, left:1150}, '
			strNetMarketFunction += '			800,'
			strNetMarketFunction += '			{onAfter:function(){'
			strNetMarketFunction += '				innerPageName = linkToLoadInner;'
			strNetMarketFunction += '				callAjaxfor("panel5",$("#_panel5").attr("href"),5000,"");'
			strNetMarketFunction += '			} '
			strNetMarketFunction += '		});'
			strNetMarketFunction += '		return false;'
			strNetMarketFunction += '	}'
			strNetMarketFunction += ')'
			
			//request quote link click
			$(".requestQuote a").click(function(){
				popUp($(this).attr("href"));
				return false;
			})
			//sub nav click 
			//sub nav click 
			$(".subNav li a").click(function(){
				var x = $(this)
				//make ajax call for page
				requestPage($(this).attr("href"), ".contentPtn", 5000, "errHref", strNetMarketFunction);
				//show spinner in div
				$(".contentPtn").html('<div class="spinner"><img src="../html/images/general/slideshowloader.gif" alt="loading" title="loading" /></div>');
				
				$(".subNav li a").removeClass("active");
				$(".subNav li a").find("img").stop().animate({opacity: 0.5}, "fast", function(){
					$(x).addClass("active");
					$(x).find("img").css("opacity","1");
				});
				return false;
			})
			
			
			break
		case "panel4":
			//web hosting page
			
			var linkToLoad = "";
			//var linkToLoadInner = "";
			$(".caseStudies div.oneItm a").click(
				
				function(){
					//alert($("#_panel5").attr("href"))
					//linkToLoad = $("#_panel5").attr("href");//
					var linkToLoadInner = $(this).attr("href");
					//errHref = "";
					//moveToId = $(this).parents("div").attr("id").replace("dl","");
					// movedToId = $(this).parents("div").attr("id");
					
					$("#panelCont").scrollTo(
						{top:855, left:1150}, 
						800,
						{onAfter:function(){
							
							innerPageName = linkToLoadInner;//"caseStudies";
							callAjaxfor("panel5",$("#_panel5").attr("href"),5000,""); //callAjaxfor(id,href,timeout)
						} 
					});
					return false;
				}
			);
			
			//request quote link click
			$(".requestQuote a").click(function(){
				popUp($(this).attr("href"));
				return false;
			})
			
			
			break
		case "panel5":
		    //Case Studies inner links
			$(".mainNav a").removeClass("active");
			$("#_panel5").addClass("active")
			 var opened = false;
			
			linkToLoad = ""; //variable to hold link to lold
			
			if(innerPageName!=""){ //inner page url
				linkToLoad = innerPageName;
				innerPageName = "";
				//alert(linkToLoad);
				$(".caseStudiesDetCont").html('<div class="spinner"><img src="../html/images/general/slideshowloader.gif" alt="loading" title="loading" /></div>');
				$(".caseStudiesDet").animate({ height: "350px" }, 
					function(){
						requestPage(linkToLoad, ".caseStudiesDetCont", 5000, "", "");
					}
				)
				// opened = true;
				
			}
			
			
			
		    var opened = false;
            
            $(".close").click(function () {
		    // alert("d");
		    	$(".caseStudiesDet").animate({ height: "0px" })
             	$(".mainCaseStudies a").removeClass("active");
				opened = false;
		    	return false;
		    })

		 var strCloseFunctions = ""
		 /*strCloseFunctions = ' $(".close").click(function () {'
		 strCloseFunctions += '$(".caseStudiesDet").animate({ height: "0px" })'
		 strCloseFunctions += 'opened = false;'
		 strCloseFunctions += 'return false;'
		 strCloseFunctions += "})"*/

		    $(".mainCaseStudies a").click(function () {
		        //alert($(this).attr("href"))
				$(".mainCaseStudies a").removeClass("active");
				$(this).addClass("active");
				linkToLoad = $(this).attr("href")
		        //<div class="close"><a href="../casestudies/default.html">back/close</a></div>
		        //requestPage($(this).attr("href"), ".caseStudiesDet", 5000, "", strCloseFunctions);
             	$(".caseStudiesDetCont").html('<div class="spinner"><img src="../html/images/general/slideshowloader.gif" alt="loading" title="loading" /></div>');
             	
				if (opened) {
                  	requestPage(linkToLoad, ".caseStudiesDetCont", 5000, "", strCloseFunctions);
		        } else {
		            $(".caseStudiesDet").animate({ height: "350px" }, 
						function(){
							//alert("df");
							//return false;
							//opened = true;
							requestPage(linkToLoad, ".caseStudiesDetCont", 5000, "", strCloseFunctions);
							}
					)
		            
		        }

		       // $(".spinner").hide();

		        opened = true;
		        return false;
		    })
			
			return false;

			break
		case "panel6":
			
			break
		case "panel7":
		    //this is faq
		   
			$(".divQuest").click(function () {
				var question = $(this);
				var answer = $(this).next();
				$(".divQuest").removeClass("activeFaq");
				$(".divAns").hide("fast"); //close all divs
				if (answer.is(':visible')) {
					answer.slideUp();
				} else {
					answer.slideDown();
					$(question).addClass("activeFaq");
				}
				return false;
			});
			
			
			var strFaqFunctions = ""
				strFaqFunctions = '$(".divQuest").click(function () {'
				strFaqFunctions += 'var question = $(this);'
				strFaqFunctions += 'var answer = $(this).next();'
				strFaqFunctions += '$(".divQuest").removeClass("activeFaq");'
				strFaqFunctions += '$(".divAns").hide("fast");'
				strFaqFunctions += "if (answer.is(':visible')) {"
				strFaqFunctions += '	answer.slideUp();'
				strFaqFunctions += '} else {'
				strFaqFunctions += '	answer.slideDown();'
				strFaqFunctions += '	$(question).addClass("activeFaq");'
				strFaqFunctions += '}'
				strFaqFunctions += 'return false;';
				strFaqFunctions += "});"									   

			$(".subNav li a").click(function(){
				var x = $(this)
				requestPage($(this).attr("href"), ".faq", 5000, "errHref", strFaqFunctions);
				//requestPage(href, div, timeout, errHref, functionToCall)
				//hide content of div
				//show spinner in div
				$(".faq").html('<div class="spinner"><img src="../html/images/general/slideshowloader.gif" alt="loading" title="loading" /></div>');
				$(".subNav li a").removeClass("active");
				$(x).addClass("active");
				return false;
			})

			break
		case "panel8":
		   // alert("df");
			//The Team
		    //About Us Accordion Effect
			
		    $(".divTit").click(function () {
            
		        var question = $(this);
		        var answer = $(this).next();
		        $(".divTit").removeClass("activeJob");

		        $(".divIntro").hide("fast"); //close all divs

		        if (answer.is(':visible')) {
		            answer.slideUp();
		        } else {
		            answer.slideDown();
		            $(question).addClass("activeJob");
		        }

		        return false;
		    });
			
			
			//careers link click
			$(".jobs a.url").click(function(){
				popUp2($(this).attr("href"));
				return false;
			})
			
			$(".polaroid").each(function (i) {
				var tempVal = Math.round(Math.random());
				//alert(tempVal)
				if(tempVal == 1) {
					var rotDegrees = randomXToY(330, 360); // rotate left
				} else {
					var rotDegrees = randomXToY(0, 30); // rotate right
				}
				
				// Internet Explorer doesn't have the "window.innerWidth" and "window.innerHeight" properties
				/*if(window.innerWidth == undefined) { 
					var wiw = 500;
					var wih = 500;
				} else {
					//var wiw = window.innerWidth;
					//var wih = window.innerHeight;	
					var wiw = 500;
					var wih = 500;
				}*/
				
				var wiw = 300;
				var wih = 250;
				//alert(wiw)
				var cssObj = { 'left' : Math.random()*(wiw),
					'top' : Math.random()*(wih),
					'-webkit-transform' : 'rotate('+ rotDegrees +'deg)',  // safari only
					'tranform' : 'rotate('+ rotDegrees +'deg)' }; // added in case CSS3 is standard
				$(this).css(cssObj);
			});
			
			// Set the Z-Index (used to display images on top while dragging)
			var zindexnr = 1;
			
			// boolean to check if the user is dragging
			var dragging = false;
			
			// Show the polaroid on top when clicked on
			$(".polaroid").mouseup(function(e){
				if(!dragging) {
					// Bring polaroid to the foreground
					zindexnr++;
					var cssObj = { 'z-index' : zindexnr,
					'transform' : 'rotate(0deg)',	 // added in case CSS3 is standard
					'-webkit-transform' : 'rotate(0deg)' };  // safari only
					$(this).css(cssObj);
				}
			});
			
			// Make the polaroid draggable & display a shadow when dragging
			$(".polaroid").draggable({
				cursor: 'crosshair',
				start: function(event, ui) {
					dragging = true;
					zindexnr++;
					var cssObj = { 'box-shadow' : '#888 5px 10px 10px', // added in case CSS3 is standard
						'-webkit-box-shadow' : '#888 5px 10px 10px', // safari only
						'margin-left' : '-10px',
						'margin-top' : '-10px',
						'z-index' : zindexnr };
					$(this).css(cssObj);
				},
				stop: function(event, ui) {
					var tempVal = Math.round(Math.random());
					if(tempVal == 1) {
						var rotDegrees = randomXToY(330, 360); // rotate left
					} else {
						var rotDegrees = randomXToY(0, 30); // rotate right
					}
					var cssObj = { 'box-shadow' : '', // added in case CSS3 is standard
						'-webkit-box-shadow' : '', // safari only
						'transform' : 'rotate('+ rotDegrees +'deg)', // added in case CSS3 is standard
						'-webkit-transform' : 'rotate('+ rotDegrees +'deg)', // safari only
						'margin-left' : '0px',
						'margin-top' : '0px' };
					$(this).css(cssObj);
					dragging = false;
				}
			});
	
	


			break
		case "panel9":
		    $(".contactForm a").click(function () {
		        popUp($(this).attr("href"));
		        return false;
		    })
			break
		case "panel10":
       		
			//news
			//alert(id);
			return false;
			break	
			
		default:
			alert(id);
			
	};
	
	if (innerPageName!=""){
		//innerPageName = ""
		
		return callPageFunctions("panel5")
		//cs = ""
		/*cs="";
		return function(){
			alert("df")	
		}	*/
	}else {
		
		
	}
	
	
}


// Function to get random number upto m
// http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
function randomXToY(minVal,maxVal,floatVal) {
	var randVal = minVal+(Math.random()*(maxVal-minVal));
	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

//function callMefunct(){ alert("dfdfdfdf");}

//
function requestPage(href, div, timeout, errHref, strSectionFunction){
	
	//ajax request for href
	// fetch content of div
	//display in div
	//alert("href = " +href + ", div = " + div + ", err url = " + errHref + ", function to call = " + strSectionFunction);
	//return false;
	dataType: "html",
	$.ajax({
		url: href,
		cache: false,
		processData: true,
		dataType: "html",
		cache: false,
		timeout: timeout,
		async: true,
		complete: function(){
		
			//alert("complete")
			
		},
		error: function(jqXHR, textStatus, errorThrown){
			//window.location.href = errHref; //redirect to page on error
			//console.log("this error occurred: "+ errorThrown);
		},
		success: function(response){
			
			
			//alert(response)
			//return false;
			try{
				var html = $('<div>').append(response).find(div).remove().html();
			//	var html = $('<div>').append(response).html();
				
				
				//alert($(div).html())
				//alert(html)
				//return false;
				if(html!=null){
					
					$(div).find(".spinner").hide("fast", function(){
						
						//return false;
						$(div).html(html+"<script>"+strSectionFunction+"</script>");
						
						
						/*$(div).html(html+"<script src='http://localhost/xdc/html/include/script/dummy.js'>"+strSectionFunction+"</script>");*/
					})
				}else {
					//window.location.href = errHref;
					//console.log(html);
				}
			}catch(err){
				//console.log("this error occurred: "+ err.decription);
				//window.location.href = errHref; //redirect to page on error
			}
		}
	});
	
	return href;
	
}





function popUp(href) {
	var width = 520;
	var height = 605;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var params = 'width=' + width + ', height=' + height;
	params += ', top=' + top + ', left=' + left;
	params += ', directories=no';
	params += ', location=0';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=no';
	params += ', status=0';
	params += ', toolbar=no';
	newwin = window.open(href, 'windowname5', params);
	if (window.focus) { newwin.focus() }
	return false;
}

function popUp2(href) {
    var width = 1000;
    var height = 600;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;
    params += ', top=' + top + ', left=' + left;
    params += ', directories=no';
    params += ', location=0';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=no';
    params += ', status=0';
    params += ', toolbar=no';
    newwin = window.open(href, 'windowname5', params);
    if (window.focus) { newwin.focus() }
    return false;
}
//write error in console of ff or alert in IE

function showEr(debug, msg){
	
	if(debug == true){
		
	}else {
		
		
	}
	
	
	
}


function animateImgsInDiv(loaderDiv, animateDiv, preLoader, imgWrapper, hideClassName){
	var pathArr = new Array();
	var imgArr = new Array();
	var imgs;
	//var result;
	try{

		$("#" + loaderDiv +"  " + imgWrapper).not("." + hideClassName).each(function(){
			pathArr.push($(this).html());
		})
		
		$("#" + loaderDiv + " img").each(function(){
			imgArr.push($(this).attr("src"));
		})
		
		imgs = "[\'" + imgArr.join("\', \'") + "\']";	
		
		//preload the images
		//append to div and cycle
		$("#" + loaderDiv).imageLoader({
		   images: eval(imgs)
		}, function(){
			$("." + preLoader).hide("fast", function(){
				$("#" + animateDiv).append(pathArr.join(" "));
				$("#" + animateDiv).cycle({
					delay:  2000, 
					speed:  500
				});
			});
		});
		//result = 1; //imgArr
	}catch(err){
		console.log(err.description)
	}
	//return result;
}

