
//check to see if site root was initialized by php(refer to /includes/header.php for this code)
if (typeof( variable ) == 'undefined' || siteRoot == "") 
{
	var siteRoot = "./"
}

//set the locations for the following image directories
var portfolioThumbDir = siteRoot+"img/portfolio_thumbs/";
var portfolioImgDir = siteRoot+"img/portfolio_imgs/";

// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
// Preload Object
var image1 = new Image(); image1.src = siteRoot+"img/home_over.jpg";
var image2 = new Image(); image2.src = siteRoot+"img/services_over.jpg";
var image3 = new Image(); image3.src = siteRoot+"img/about_over.jpg";
var image4 = new Image(); image4.src = siteRoot+"img/request_over.jpg";
var image5 = new Image(); image5.src = siteRoot+"img/portfolio_over.jpg";
var image6 = new Image(); image6.src = siteRoot+"img/resources_over.jpg";
var image7 = new Image(); image7.src = siteRoot+"img/bg_chrome.png";
var image8 = new Image(); image8.src = siteRoot+"img/call-action-over.jpg";


function contentRotators() {
	//for blogs on homepage
	if($('#blog_rotator').length)
	{
		$('#blog_rotator').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 5000
		});
	}

	//news in right sidebar
	$('#newsRotator ul').cycle({ 
		fx:     'scrollUp', 
		speed:  2000, 
		timeout: 10000, 
		next:   '#news-next', 
		prev:   '#news-prev' 
	});

}

/* get URL Parameters*/
function gup( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

//actions for showHide of article pages
//show / hide by clicking a menu - only one "item" visible at once
function articleActions() {
	//if there are any articleItems
	if($("#articleItems").length) {
		$(".articleItem").hide();
		$(".articleItem:first").show();
		
		$("#articleList a").click(function(){
			$(".articleItem").hide();
			var selectedItem = $(this).attr("rel");
			$("#"+selectedItem).fadeIn("slow");
			return false;
		});

		//if article is accesed directly
		var selected = gup("article");
		if( selected != "") {
			setTimeout( function(){ $("a[href=#"+selected+"]").click(); }, "1500");
		}
	}
}

function supercrazyie8funtimefix()
{
	$('div.toggler-c').each(function(){
		if($(this).css("display") == "block") {
			$(this).css("position", "static");
		}
	});
}

//actions for showHide of news pages
//(a show / hide by clicking h4's, multiple "items" showing at once)
function newsActions() {
	$('div.toggler-c').toggleElements({ 
		fxAnimation:'slide', 
		fxSpeed:'slow', 
		className:'toggler', 
		onShow:supercrazyie8funtimefix
	});
	
	var selected = gup("article");
	//open an article if the argument is there
	if( selected != "")
	{
		setTimeout( function(){ $("div#"+selected).prev("a.toggler").click(); }, "1500");
	}
}

//fade form status after 'x' seconds
function fadeoutFormStatus()
{
	if($('#formStatus p').text() != "")
	{
		$('#formStatus')
		.animate({height:"0",width:"10%"}, 
				{queue:false, duration:5000})
		.animate({opacity:"0", paddingTop:"0", paddingBottom:"0", marginTop:"0", marginBottom:"0"}, 
				{queue:false, duration:7000});		
						
		$('#formStatus p')
		.animate({height:"0", width:"80%", paddingTop:"0", paddingBottom:"0", marginTop:"0", marginBottom:"0",  opacity:"0"}, 
				{queue:false, duration:6000})
		.animate({fontSize:"8px", lineHeight:"8px"},{queue:false, duration:5000});
	}
}

//Portfolio_Nav Actions
function resetPortfolioSelector()
{
	$("#portfolio_title a").removeClass("hover");
	$("#portfolio_selector").hide();
	$("#portfolio_thumbs").fadeTo("slow", 1.00);
}

//Insert an error into the page if ajax bites it
function ajaxError(content)
{
	$("#portfolio_thumbs").html(content+"<br/><br/><br/><br/><br/><br/>");
	resetPortfolioSelector();
}

//create the markup for thumbs
function formatThumbs (id, client, thumb)
{
	return 	"<a href=\"#\" rel=\"port_"+id+"\" title=\""+client+"\" > " +
	"	<img src=\""+portfolioThumbDir+thumb+"\" alt=\""+client+"\" />" +
	"	<br/>"+
	"</a>";
}

//create the markup for details
function formatDetails (id, client, url, img, desc)
{
	return	"<div id=\"port_"+id+"\" class=\"portfolio_detail\">" + 
	"	"+img +
	"	<h5>"+client+"</h5>" +
	"	"+url+"<br/>" +
	"	<div class=\"description\">"+desc+"</div>" +
	"</div>";
}

function portfolioActions()
{
	//actions to:
	//show the portfolio selector
	if($("#portfolio_title a").length)
	{
	$("#portfolio_title a").click(function(){
		if(!$("#portfolio_selector").is(":visible"))
		{
			$("#portfolio_title a").addClass("hover");
			$("#portfolio_thumbs").fadeTo("medium", 0.10);
			$("#portfolio_selector").fadeIn("fast");
		}
		else
			resetPortfolioSelector();

		return false;
	});
	}
	
	//clicking on a portfolio icon will trigger the same
	//event as a portfolio selector
	if($("#portfolio_icons a").length){
	$("#portfolio_icons a").click(function()
	{
		var section = $("#portfolio_selector a[rel="+$(this).attr("rel")+"]");
		section.trigger("click");
		return false;
	});
	}
	
	//actions to:
	//select and show the associated thumbnails
	//hide the portfolio selector
	if($("#portfolio_selector a").length){
	$("#portfolio_selector a").click(function()
	{
		var title = $(this).text();
		var category = $(this).attr("rel") + "";
		var thumbs = ""; //this is the string of html for the thumbnails nav
		var details = "";//this is the string of html for the main content
		
		//AJAX call to xml
		$.ajax({
			type: "GET",
			url: "includes/portfolio.xml",
			dataType: "xml",
			error: function(errorData){
				alert(errorData);
			},
			success: function(xml){					
				var count = 0;
				//hide the container for the content
				//$("#midLeft").hide();
				
				$(xml).find("portfolio").each(function(){
				//get the category (designs, brandings, applications)
				$(this).find( category ).each(function(){
				
				//get content of each item					
				$(this).find("item").each(function(){
					var id = $(this).find('id').text();
					var client = $(this).find('client').text();
					var url = $(this).find('url').text();
					var urltxt = url.substr(7,url.length);
					var img = $(this).find('image').text();
					var thumb = $(this).find('thumb').text();
					var desc = "";//$(this).find('description').text();	
				
					//check for blank entries and handle
					if(url != "" || url != "http://")
						url = "<a href=\""+url+"\" target=\"_blank\">"+urltxt+"</a>";
					if(img != "")
						img = "<img src=\""+portfolioImgDir+img+"\" alt=\""+client+"\" />";
					if(desc == "")
						desc = "";//add alternate description message here if message is blank	   				
					
					count++;
					
					thumbs = thumbs + formatThumbs(id, client, thumb);
					details = details + formatDetails(id, client, url, img, desc);
				});
				});
				});
				
				//set properties in the main content area
				//$("#midLeft").fadeIn("slow");
				$("#midLeft h2").text(title+" portfolio");
				
				//set properties in the portfolio nav
				$("#portfolio_thumbs").html(thumbs);
				
				//EVENT for clicking on a thumbnail		
				$("#portfolio_thumbs a").click(function(){
					var port = "#"+($(this).attr("rel"));
					//if portfolio exists, show or fade in and out if it is already showing for some 
					//visual feed back to show user they have clicked something
					if($(port).length)
					{
						if(!$(port).is(":visible"))
						{
							$(".portfolio_detail").hide();
							$(port).fadeIn("fast");
						}else{
							$(port).fadeOut("fast", function(){ $(port).fadeIn("medium"); });
						}
					}
					
					return false;	
				});
				
				// Change portfolio drop down to show the selected category
				$("#portfolio_title a").html( title );
				
				resetPortfolioSelector();
				
				$("#portfolio_details").html(details);				
				$(".portfolio_detail:first").show();
			},
			timeout: function(){ ajaxError("Timeout!") },
			error: function(){ ajaxError("Ajax Error!") },
			parsererror: function(){ ajaxError("Parse Error!") }
		 });

		return false;
	});
	}
	
	//actions to:
	//Mouse out of the right content, hide the portfolio selector
	if($("#portfolio_selector").length)
	{
		$("#midRight").bind("mouseleave",function(){
			if($("#portfolio_selector").is(":visible"))
				resetPortfolioSelector();
		});
	}
}

function requestFormActions()
{
	if($("#requestForm").length)
	{
		$("#requestForm").validate({ 
			rules: {
				name: "required",
				company: "required",
				position: "required",
				email: {required: true, email: true},
				referred: "required",
				security_code: "required"
			}, messages: {
				name: "Please enter your name.",
				company: "Please enter the name of your company.",
				position: "Please specify your position within your company.",
				email: "Please enter a valid email address.",
				referred: "Please select where you have heard of us.",
				security_code: "Please enter the security code listed above this field."
			}
		});
	}
	
	//datePicker on the request page
	if($("#time1").length)
		$("#time1").clockpick({
			starthour : 8,
			endhour : 17,
			showminutes : false,
			event : 'mouseover',
			hoursopacity : 0.90
		});
		
	//datePicker on the request page
	if($("#time2").length)
		$("#time2").clockpick({
			starthour : 8,
			endhour : 17,
			showminutes : false,
			event : 'mouseover',
			hoursopacity : 0.90
		});
}

function initFlashElements()
{

	if($("#statementFlash").length)
	{
		swfobject.embedSWF(siteRoot+"flash/statement-flash.swf", "statementFlash", "554", "48", "8.0.0", expressInstall, flashvars, params, attributes);
	}
	if($("#newsFlash").length)
	{
		swfobject.embedSWF(siteRoot+"flash/spiderNews.swf", "newsFlash", "210", "75", "8.0.0", expressInstall, flashvars, params, attributes);
	}
	if($("#clientFlash").length)
	{
		swfobject.embedSWF(siteRoot+"flash/clientFlash.swf", "clientFlash", "210", "55", "8.0.0", expressInstall, flashvars, params, attributes);
	}
	if($("#portfolioFlash").length)
	{
		params.base = siteRoot+"flash/portfolio/";
		swfobject.embedSWF(siteRoot+"flash/portfolio/spiderPort.swf", "portfolioFlash", "210", "164", "8.0.0", expressInstall, flashvars, params, attributes);
	}
}

function webmailForm()
{
	// Webmail login
	$("#webmail").click(function()
	{
		if($("#webmailLogin").is(":visible"))
		{
			$("#webmailLogin").animate({top:"-1", height:"0"},{queue:false, duration:1000 }).fadeOut(1000);
		}
		else
		{
			if(navigator.appName == "Microsoft Internet Explorer")
				$("#webmailLogin").fadeIn("fast").animate({top:"-70", height:"70"},{queue:true, duration:700 }).removeAttr("style")
			else
				$("#webmailLogin").fadeIn("fast").animate({top:"-72", height:"70"},{queue:true, duration:700 }).removeAttr("style")
		}
		return false;		
	});
	
	$("#webmailLogin a.close").click(function()
	{
		$("#webmail").trigger("click");
		return false;
	})
	return false;
}

function removeInlineStyles($element)
{
	$(element).removeAttr("style");
}


//for error reporting on ajax functions
$(document).ajaxError(function(){
    if (window.console && window.console.error)
        console.error(arguments);
});


//Declaired here for inline js for embedding fonts
var flashvars = {};
var params = {
		menu: "false",
		wmode: "transparent",
		base: siteRoot+"flash/"
	};
var attributes = false;
var expressInstall = siteRoot+"flash/expressInstall.swf";


/** When dom ready **/
$(document).ready(function() 
{
//hide elements with javascript
	$("#seoFoot").hide();
	
//init slideshow
	contentRotators();	
//init portfolio actions
	portfolioActions();	
//newsPage items
	newsActions();
//article actions, (resources page)
	articleActions();
//form actions
	requestFormActions();
//webmail login
	webmailForm();
//swfObject for all flash elements
	initFlashElements();
// PNG Fix
	$(document).pngFix();
	
	$("#sitemap_link").click(function()
	{		
		if($("#seoFoot").is(":visible"))
		{
			$("#seoFoot").animate({height:"0"},{queue:false, duration:1500, easing:"swing"}).fadeOut(1500);
		}else{
			$("#seoFoot").css({height:""});
			$("#seoFoot").fadeIn(800);
			$.scrollTo("#seoFoot", 1000);
		}
		return false;
	});

});

