//***********************************************
//
//jQuery functions
//
//***********************************************

$(document).ready(function(){
	
	//Temp Function for Maintenance Period
	//window.location="http://www.jakgraphicdesign.com/maintenance.html"
	
	//Load Footer
	$('#footer').append('<img style="float:right;" src="images/content/WBENC-wlogo.gif" alt="WBENC-wlogo" width="35" height="35"/><p>&copy;2010 JAK Graphic Design, LLC</p><br />');
	
	
	//Load Color Picker
	$('#colorbar').css('display','none');
	$('#colorbar').html('<a id="colortab" href=""><img src="images/layout/colorbar/tab.png" alt="tab" width="195" height="50" border="0"/></a>'+
		'<div id="colorOpts">'+
			'<strong>Select a color swatch</strong>'+
			'<a href="lib/core/purple.css"><img src="images/layout/colorbar/purple.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/red.css"><img src="images/layout/colorbar/red.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/grey.css"><img src="images/layout/colorbar/grey.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/pink.css"><img src="images/layout/colorbar/pink.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/orange.css"><img src="images/layout/colorbar/orange.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/peach.css"><img src="images/layout/colorbar/peach.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/blue.css"><img src="images/layout/colorbar/blue.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/gold.css"><img src="images/layout/colorbar/gold.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/black.css"><img src="images/layout/colorbar/black.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'<a href="lib/core/green.css"><img src="images/layout/colorbar/green.jpg" alt="color swatch" width="20" height="20" border="0"/></a>'+
			'</div>').slideDown('normal',function(){
				
				$('#colortab').toggle(function(){
					$('#colorbar div').slideDown('fast');
					},
				function(){
					$('#colorbar div').slideUp('fast');
					});	
	
				$('#colorbar div a').click(function(){
					$('#colorSheet').attr('href',$(this).attr('href'));
					setCookie($(this).attr('href'));
					return false;
					});
				
				});
	
	//Read in the cookie to determine the colorspace
	readCookie();
	
	//Check client browser and respond accordingly
	if ($.browser.msie && $.browser.version < 7) {
		$('#colorbar').remove();
		//$('#colorbar').css('display','none');
		window.location="http://www.jakgraphicdesign.com/oldSite/";
		}//end if
	
	else if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		window.location="http://www.jakgraphicdesign.com/iphone";
		}
		
	else if($.browser.msie && $.browser.version > 6){
		
		//Portfolio changes using a sliding function because of IE's
		//transparency/alpha problems
		$('#portfolioTab li').click(function(){
			$(this).attr('class','current');
			newSample="#"+$(this).attr('title');
			
			$('#portfolio div').each(function(){;
				if($(this).css('display')!="none"){
					$('#portfolioTab').find("[title="+$(this).attr('id')+"]").removeAttr('class');
					
					$(this).slideUp(function(){
						$(newSample).slideDown();
						});
					}//end if
				});
			
			return false;
			});
		
		}//end else if
		
	else{
		
		//Portfolio changes using a fade function
		$('#portfolioTab li').click(function(){
			$(this).attr('class','current');
			newSample="#"+$(this).attr('title');
			
			$('#portfolio div').each(function(){;
				if($(this).css('display')!="none"){
					$('#portfolioTab').find("[title="+$(this).attr('id')+"]").removeAttr('class');
					
					$(this).fadeOut(function(){
						$(newSample).fadeIn();
						});
					}//end if
				});
			
			return false;
			});
		
		}//end else
		
		$('#portfolio div a,#featuredWork ul li a').fancybox({
		
			'titleShow'     : false,

		
		});
		//$('#portfolio div a,#featuredWork ul li a,a[class*=.staffBio]').slimbox();
		
		$('a[class*=.staffBio]').fancybox({
			//'frameWidth':650,
			//'frameHeight':600,
			'width' : 675,
			'height' : 640,
			'titleShow'     : false,
			'type' : 'iframe'
			});
		
		$('#sendForm').click(function(){
			
			var isEmail=false;
			
			//Gets information from form to send to server
			var username=$('#nameInput').val();
			var useremail=$('#emailInput').val();
			var userphone=$('#phoneInput').val();
			var usercomment=$('#messageInput').val();
			var fileAttachment=$('#fileUpload').val();
			//alert(fileAttachment);
			var toPass="";
			
			if(document.getElementById('generalContact')){
				//Creates data string to send to server
				toPass="type=general&name="+username+"&email="+useremail+"&phone="+userphone+"&message="+usercomment+"&attachment="+fileAttachment;
				}
			else if(document.getElementById('hireUs')){
				//Gets information from form to send to server
				var companyname=$('#companyNameInput').val();
				var companyurl=$('#companyUrlInput').val();
				var budget=$('#budgetInput').val();
				var deadline=$('#deadlineInput').val();
				var projecttype=$('#projectInput').val();
				
				//Creates data string to send to server
				toPass="type=hire&company="+companyname+"&companyurl="+companyurl+"&name="+username+"&email="
				+useremail+"&phone="+userphone+"&projecttype="+projecttype+"&budget="+budget+"&deadline="+deadline+"&message="+usercomment;
				}
			
			else{return;}
			
			for(e=0;e<useremail.length;e++){
				if(useremail.charAt(e)==="@"){isEmail=true;break;}
				}//end for
			
			//Display error message
			if (username==="" || useremail==="" || usercomment===""){
				alert("Please complete all fields.");
				return false;
				}//end if
			
			else if(isEmail===false){alert("Please enter a valid email address.");}
			
			else{
			
				$.ajax({  
	  				type: "GET", 
	  				url: "http://www.jakgraphicdesign.com/lib/core/sendForm.asp",  
	  				data: toPass,  
	  				success: function(data) {
	    				alert("Your message has been sent successfully!");
	    				$('#nameInput').val("");
	    				$('#emailInput').val("");
	    				$('#phoneInput').val("");
	    				$('#messageInput').val("");
	    				$('#companyNameInput').val("");
						$('#companyUrlInput').val("");
						$('#budgetInput').val("");
						$('#deadlineInput').val("");
						$('#projectInput').val("");
	  					},//end success
	  				
	  				error: function(e,f,g){
	  					alert("Sorry an error occured. Your message couldn't be delivered at this time. Please try again later.");
	  					$('#nameInput').val("");
	    				$('#emailInput').val("");
	    				$('#phoneInput').val("");
	    				$('#messageInput').val("");
	    				$('#companyNameInput').val("");
						$('#companyUrlInput').val("");
						$('#budgetInput').val("");
						$('#deadlineInput').val("");
						$('#projectInput').val("");
	  					}//end error  
					});  
					return false
				}//end else
				
			});//end #sendForm click function
	
	});//end document.ready

//Delete Cookie
function deleteCookie(){
	var date=new Date();
	date.setTime(date.getTime()+(-1*24*60*60*1000));
	
	document.cookie = "colorsheet=null; expires="+date.toGMTString()+"; path=/";
	}//end setCookie	

function setCookie(colorSpec){
	var date=new Date();
	date.setTime(date.getTime()+(7*24*60*60*1000));
	deleteCookie();
	document.cookie = "colorsheet="+colorSpec+"; expires="+date.toGMTString()+"; path=/";
	}//end setCookie
	
function readCookie(){
	//alert(document.cookie);
	if(document.cookie!=""){
		var siteCookie="";
		for(i=document.cookie.length;i>0;i--){

			if(document.cookie.charAt(i)=="="){
				break;
				}//end if
			
			else{
				siteCookie=document.cookie.charAt(i)+siteCookie;
				}//end else
		
			}//end for loop

		if(siteCookie.substr(siteCookie.length-3,siteCookie.length)=="css" && siteCookie.length>13){
			$('#colorSheet').attr('href',siteCookie);
			}//end if
			
		else{
			$('#colorSheet').attr('href','lib/core/purple.css');
			setCookie('lib/core/purple.css');
			}
		
		}//end if
	
	}//end readCookie

function removeColorBar(){
	
	$('#colorbar').remove();
	
	}//end of removeColorBar
