// JavaScript Document
$(document).ready(function() {
//SLIDER VARIABLES
var global_wrap_id = "0";
var global_orb = 0;
var active_scroll = 0;
var activeid = "0";
var totalsize = "0";
var totalclicks = "0" ;

	//LOAD XML FILE
	$.ajax({
		type: "GET",
		url: "pics.xml",
		dataType: "xml",
		success: function(xml) {
			
			$(xml).find("orb").each(function(index) {
				var image_array = new Array();
				var image_array_big = new Array();
				var image_array_text = new Array();
				var image_array_title = new Array();
				var image_array_logo = new Array();
				var image_array_logo_title = new Array();
				
				var counter = 0;
				var img_class = 0;
				
				var orbid = index;
				$(".scrollable_wrapper").append("<div class='global_orb_wrapper orb_wrapper" + orbid + "'><a class='prev" + orbid + " browse'></a><div class='scrollable scrollable" + orbid + "'></div><a class='next" + orbid + " browse'></a></div>");
				$(".scrollable" + orbid).append("<div class='items" + orbid + " inactive' id='orb" + orbid + "'></div>");
				$(".items" + orbid + "#orb" + orbid).append("<div class='img_wrap img" + img_class + "'>");
				var total = $(this).find("image").size() ;
				
				$(this).find("image").each(function(index) {
						if(counter >= 3)
						{
						var url = $(this).find("url").text();	
						image_array.push(url);
						$(".items" + orbid + "#orb" + orbid + " .img" + img_class).append("<span class='loading global image" + index + "'><input name='" + index + "' type='hidden' value='" + orbid + "' /></span>");	
						img_class = img_class + 1;
						$(".items" + orbid + "#orb" + orbid).append("</div>");
						if(index == total -1)
						{}
						else {
						$(".items" + orbid + "#orb" + orbid).append("<div class='img_wrap img" + img_class + "'>");	
						}
						counter = 0;
						}
						else {
						var url = $(this).find("url").text();	
						image_array.push(url);
						$(".items" + orbid + "#orb" + orbid + " .img" + img_class).append("<span class='loading global image" + index + "'><input name='" + index + "' type='hidden' value='" + orbid + "' /></span>");
						counter = counter + 1;
						}
						
						$(this).find("big_url").each(function() {
							var big_url = $(this).text();	
							image_array_big.push(big_url);
						});
						
						$(this).find("logo").each(function() {
							var logo = $(this).text();
							image_array_logo.push(logo);
						});
						
//						$(this).find("title").each(function() {
//							var logo_title = $(this).find("i").text();
//							image_array_logo_title.push(logo_title);
//						});
						
						$(this).find("content").each(function() {
							var str = "";
							var title = $(this).find("title").attr("name");
							image_array_title.push(title);
							$(this).find("line").each(function() {
								str += $(this).text();
								//str += "<br />";
							});
							image_array_text.push(str);
						});	
						
							$(".work_wrap").append('<div class="orb' + orbid + ' loading img_content_wrap" id="img_content_wrap_orb' + orbid + '_image' + index + '"><div class="image"></div><div class="text"><img src="' +  image_array_logo[index] + '" alt="' + image_array_title[index] +'" title="' + image_array_title[index] + '" border="0" />' + image_array_text[index] + '</div></div>');
						
				}); //end of IMAGE loop
			$(".items" + orbid + "#orb" + orbid).append("</div>");
			
			//appending images to the spans created
				$("#orb" + orbid + " .global").each(function(index) {
				var id = $("input",this).attr("name");
					var img = new Image();
					$(img).load(function() {
					
						$(this).hide();
						$("#orb" + orbid + " .image" + id).removeClass("loading").append(this);
						//alert($("#orb" + orbid + " #" + id).html());
						$(this).fadeIn();
						if(index == 0)
						{
								$(".global img").stop().animate({ opacity: 0.4 }, 300);
							$("#orb0 .image0 img").stop().animate({ opacity: 1.0 }, 300);	
						//$("#orb" + orbid + " .image0 img").css({"border" : "1px solid #f59b00"});	
						}
					}).attr("src", image_array[index]).attr("title",image_array_title[index]);
						
//						if(index == 0)
//						{
//						$("#orb" + orbid + " .image" + global_wrap_id + " img").css({"border" : "1px solid #f00"});	
//						}
//						$(this).removeClass("loading");
//						$(this).append("<img src='" + image_array[index] + "' border='0' alt='" + image_array[index] + "' title='" + image_array[index] + "' />" ).fadeIn();
				
				});
				
				//large images load	
					$(".orb" + orbid).each(function(index) {
						
											
						var id = $(this).attr("id");
						
							var img = new Image();
							$(img).load(function() {
								$(this).hide();
								$("#" + id).removeClass("loading");
								$("#" + id + " .image").append(this);
								$(this).fadeIn();
							}).attr("src", image_array_big[index]).attr("title",image_array_title[index]);
					});
							
				$(".scrollable" + orbid).scrollable({prev:".prev" + orbid,next:".next" + orbid});

					
			
			}); // end of ORB loop

			
			//thumbnail mouse over
		$(".global").hover(function() {
		var id = $("input",this).attr("name");
		
		$(".global img").stop().animate({ opacity: 0.2 }, 300);
		$("#orb" + global_orb + " .image" + id + " img").stop().animate({ opacity: 1.0 }, 300);	
		},function(){ 
		var id = $("input",this).attr("name");
		
		$(".global img").stop().animate({ opacity: 0.4 }, 300);
		$("#orb" + global_orb + " .image" + id + " img").stop().animate({ opacity: 1.0 }, 300);	
		});
		
		//thumbnail mouse out
		$(".global").mouseout(function() {

		var id = $("input",this).attr("name");

		$(".global img").stop().animate({ opacity: 0.2 }, 300);
		$("#orb" + global_orb + " .image" + id + " img").stop().animate({ opacity: 1.0 }, 300);	
		
		$(".global img").stop().animate({ opacity: 0.4 }, 300);
		$("#orb" + global_orb + " .image" + activeid + " img").stop().animate({ opacity: 1.0 }, 300);	
		
		});
		
		
		//thumbnail click
		$(".global").click(function() {
			var id = $("input",this).attr("name");
			var value = $("input",this).attr("value");
			activeid = id;
			$("#img_content_wrap_orb" + global_orb + "_image" + global_wrap_id).hide();
			$("#img_content_wrap_orb" + value + "_image" + id).fadeIn("slow");
			
			//$("img",this).css({"border" : "1px solid #f59b00"});
			if(id == global_wrap_id)
			{}
			else {
			$("#orb" + global_orb + " .image" + global_wrap_id + " img").css({"border" : "0px solid #ccc"});
			}
			global_wrap_id = id;
			global_orb = value;
			activeid = id;
			totalsize = $("#orb" + global_orb + " img").size();
			totalclicks = (totalsize / 4) - 1;

			$(".global img").stop().animate({ opacity: 0.2 }, 300);
			$("#orb" + global_orb + " .image" + id + " img").stop().animate({ opacity: 1.0 }, 300);	

			if(activeid < totalsize -1)
			{
			$(".forward").show();
			}
			else {
				$(".forward").hide();	
			}
			
			if(activeid <=0)
			{
				$(".backward").hide();
			}
			else {
				$(".backward").show();
			}
			return false;
		});
		
		
		//menu click
		
		$(".a_orb").click(function() {
		$("#orb" + global_orb).removeClass().addClass("items" + global_orb + " inactive").hide();
		$(".orb_wrapper" + global_orb).hide();
		var id = $("input",this).attr("value");
		$(".items#orb" + active_scroll).hide();
		$(".items" + id + "#orb" + id). fadeIn();
		$(".scrollable" + global_orb + " img").css({"border" : "0px solid #ccc"});				
		//$("#orb" + global_orb + " .image0 img").css({"border" : "1px solid #f59b00"});
		$(".orb" + active_scroll).hide();
		$("#img_content_wrap_orb" + id + "_image0").fadeIn();
		global_wrap_id = "0";
		global_orb = id;
		
		$(".global img").stop().animate({ opacity: 0.4 }, 300);
		$("#orb" + global_orb + " .image0 img").stop().animate({ opacity: 1.0 }, 300);			
		
		active_scroll = id;
		temp_size = 0;
		activeid = 0;
		totalsize = $("#orb" + global_orb + " img").size();
		totalclicks = (totalsize / 4) - 1;
				
		$("#orb" + id).removeClass().addClass("items");
		$(".orb_wrapper" + id).fadeIn();		
		if(totalsize <=4)
		{
		$(".next" + global_orb).addClass("disabled");
		$(".prev" + global_orb).addClass("disabled");
		}
		else {
			$(".next" + global_orb).removeClass("disabled");
		}
		$(".forward").show();
		$(".backward").hide();
			
		return false;
		
		});
		
		
		//FORWARD
		$(".forward").click(function() {
		totalsize = parseInt($("#orb" + global_orb + " img").size());
		totalclicks = parseInt((totalsize / 4) - 1);
//alert(activeid);
//alert(totalclicks);
//alert(totalsize);
		if(activeid < totalsize - 1)
		{
		activeid = parseInt(activeid) + 1;
		$(".backward").show();
		}
		else {
		$("#orb" + global_orb + " .image" + activeid + ".global").next().click();
		$(this).hide();	
		}
		
		$("#orb" + global_orb + " .image" + activeid + ".global").click();
		
		if((parseInt(activeid) % 4) == 0)
		{
		$(".next" + global_orb).click();
		}
		return false;
		});
		
		
		//BACKWARD
		$(".backward").click(function() {
		totalsize = $("#orb" + global_orb + " img").size();
		totalclicks = (totalsize / 4) - 1;
		$(".forward").show();
		
		if(activeid > 1)
		{
		if((activeid % 4) == 0)
		{
		$(".prev" + global_orb).click();
		}
		activeid = parseInt(activeid) - 1;	
		$("#orb" + global_orb + " .image" + activeid + ".global").click();
		}
		else {
		$("#orb" + global_orb + " .image" + activeid + ".global").prev().click();
		$(this).hide();	
		}

		return false;
		
		});
		
		$("#orb" + global_orb).removeClass().addClass("items");
		$(".items:first").fadeIn();
		$(".img_content_wrap:first").fadeIn();
		$(".orb_wrapper" + global_orb).show();
		$(".backward").hide();		
		/**
		if($("items:first img").size() <= 4)
		{
		$(".next0,.prev0").css({"background" : "none"});
		}**/
		
		
//ROTATOR VARIABLES

	//CONTACT FORM
	//$('#Contact').hide();
	$('.ContactBtn').click(function() {
		$('#Contact').slideToggle();
		$('html, body').animate( { scrollTop: 0 }, 'slow' );
		return false;
	});
	
	// MAGIC LINE
	 var $el, leftPos, newWidth, $mainNav = $("#example-one");
	 $mainNav.append("<li id='magic-line'></li>");
	 
	 var $magicLine = $("#magic-line");
	 
	 $magicLine
	 .width($(".current_page_item").width()-16)
	 .css("left", $(".current_page_item a").position().left)
	 .data("origLeft", $magicLine.position().left)
	 .data("origWidth", $magicLine.width());
	 
	 $("#example-one li").find("a").hover(function() {
	 $el = $(this);
	 leftPos = $el.position().left;
	 newWidth = $el.parent().width()-16;
	 
	 $magicLine.stop().animate({
	  left: leftPos,
	  width: newWidth
	 });
	 }, function() {
	 $magicLine.stop().animate({
	  left: $magicLine.data("origLeft"),
	  width: $magicLine.data("origWidth")
	 }); 
	 });
	
	//WORK MENU ITEM HOVER
	$("#work_nav li a").hover(function() {
	if($(this).parent().attr("class") != "active")
	{
	$(this).parent().css({"background-position" : "0 -67px"});
	}
	}, function() {
			if($(this).parent().attr("class") != "active")
	{
		$(this).parent().css({"background-position" : "0 0px"});
	}
	});

	$("#work_nav li a").focus(function() {
	if($(this).parent().attr("class") != "active")
	{
	$(this).parent().css({"background-position" : "0 -67px"});
	}
	}, function() {
			if($(this).parent().attr("class") != "active")
	{
		$(this).parent().css({"background-position" : "0 0px"});
	}
	});

	$("#work_nav li a").blur(function() {
	if($(this).parent().attr("class") != "active")
	{
	$(this).parent().css({"background-position" : "0 0px"});
	}
	}, function() {
			if($(this).parent().attr("class") != "active")
	{
		$(this).parent().css({"background-position" : "0 -67px"});
	}
	});

	//WORK MENU ITEM CLICK
	$("#work_nav li a").click(function() {
		$("#work_nav li").each(function() {
		$(this).removeClass("active").removeAttr("style");
		$(this).parent().css({"background-position" : "0 0px"});
		});
		if($(this).parent().attr("class") != "active")
		{
		$(this).parent().addClass("active");
		//$(this).parent().css({"background-position" : "0 -134px"});
		}
		return false;
	});
	

		
		} //end of SUCCESS		
		
	}); // end of AJAX
	
	
	//EMAIL AJAX CALL
	$(".submit-button").click(function() {
		var name = $("#Name").val();
		var email = $("#Email").val();
		var message = $("#Message").val();
		var target = "mail.php";
		if(name != "" && email != "" && message != "")
		{
			
			if (echeck(email)==false){
				$("#Email").val("");
				$("#Email").focus();
				return false;
			}
			else {
				$(".contact_error").text("");
				$(this).attr("disabled","disabled");
				$(this).attr("value","Sending...");
				$.ajax({
					type: "POST",
					url: target,
					data: "name=" + name + "&email=" + email + "&message=" + message,
					success: function(msg) {
						if(msg ==1)
						{
						$(".submit-button").removeAttr("disabled");
						$(".submit-button").attr("value","Submit");
						alert("Email sent Successfully. We will get back to you soon.");
						$("#Name").val(null);
						$("#Email").val(null);
						$("#Message").val(null);
						$('#Contact').slideToggle();
						}
						else {
						$(".contact_error").text("Email not sent. Please try again later.");	
						$(".submit-button").attr("value","Submit");
						}
					}
				});
			}
		}
		else {
		$(".contact_error").text("Please fill all the fields");
		return false;
		}
		return false;
	});
	

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   $(".contact_error").text("Invalid E-mail");
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   $(".contact_error").text("Invalid E-mail");
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    $(".contact_error").text("Invalid E-mail");
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    $(".contact_error").text("Invalid E-mail");
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    a$(".contact_error").text("Invalid E-mail");
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    a$(".contact_error").text("Invalid E-mail");
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    $(".contact_error").text("Invalid E-mail");
		    return false
		 }

 		 return true					
	}
	
	$(".skip").click(function() {
		var pos = $(".Text").position();
		$('html, body').animate( { scrollTop: pos.top + 'px' }, 'slow' );
		$(".skiptocontent").click().focus();
	});
	
	
	
	


							 
}); // end of LOAD


//$(function(){
//
//    var $el, leftPos, newWidth,
//        $mainNav = $("#example-one");
//
//	$mainNav.append("<li id='magic-line'></li>");
//    
//    var $magicLine = $("#magic-line");
//    
//    $magicLine
//        .width($(".current_page_item").width())
//        .css("left", $(".current_page_item a").position().left)
//        .data("origLeft", $magicLine.position().left)
//        .data("origWidth", $magicLine.width());
//        
//    $("#example-one li").find("a").hover(function() {
//        $el = $(this);
//        leftPos = $el.position().left;
//        newWidth = $el.parent().width();
//        
//        $magicLine.stop().animate({
//            left: leftPos,
//            width: newWidth
//        });
//    }, function() {
//        $magicLine.stop().animate({
//            left: $magicLine.data("origLeft"),
//            width: $magicLine.data("origWidth")
//        });    
//    });
//    
//    
//});




