﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}


//disabling popup with jQuery magic!
function disablePopupSend(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut();
		$("#popupContact").fadeTo(3000,1).fadeOut("slow");
		popupStatus = 0;
	}
}
//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#popup").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

//Contact Form
var divid = 'contactArea';
var loadingmessage = '<br />Your message has been sent!';

function AJAX(){
	
	var xmlHttp;
	try{
	xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	return xmlHttp;
	}
	catch (e){
	try{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
	return xmlHttp;
	}
	catch (e){
	try{
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     return xmlHttp;
	}
	catch (e){
	alert("Your browser does not support AJAX!");
	return false;
	}
	}
	}

}

   function formget(f, url) {

      var poststr = getFormValues(f);
      postData(url, poststr);

   }

function postData(url, parameters){

var xmlHttp = AJAX();

      xmlHttp.onreadystatechange =  function(){
      if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){  
			document.getElementById(divid).innerHTML=loadingmessage;  
		}  
            if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
 				
 				document.getElementById(divid).innerHTML=xmlHttp.responseText;
          
         } else {
            ;
         }
      } 
      
      }
      xmlHttp.open("POST", url, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);
   }

   function getFormValues(fobj)

    {
       var str = "";
       var valueArr = null;
       var val = "";
       var cmd = "";

       for(var i = 0;i < fobj.elements.length;i++)

       {
           switch(fobj.elements[i].type)

           {
               case "text":

                    str += fobj.elements[i].name +
                     "=" + escape(fobj.elements[i].value) + "&";
                     break;
                
               case "textarea":

                    str += fobj.elements[i].name +
                     "=" + escape(fobj.elements[i].value) + "&";
                     break;

               case "select-one":

                    str += fobj.elements[i].name +
                    "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
                    break;

           }
       }

       str = str.substr(0,(str.length - 1));
       return str;
       
       
       }

/**
//thanks to Ben Sterling for the shuffle     
(function($) {
	$.fn.jqShuffle = function(options){
		return this.each(function(){
			var $this = $(this), $els = $this.children().css({left:0,top:0}), els = $els.get();
			if (els.length < 2) return; // don't bother
			var opts = $.extend({}, $.fn.jqShuffle.defaults, options);

			// allow shorthand overrides of width, height and timeout
			var cls = this.className;
			var w = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width
			var h = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
			if ($this.css('position') == 'static') $this.css('position', 'relative');
			if (w) $this.width(w);
			if (h && h != 'auto') $this.height(h);
			opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;
			$els.each(function(i){$(this).css('z-index', els.length-i);}).css('position','absolute');

            opts.coords = opts.coords || {left:-$this.width(), top:15};
            opts.els = [];
            for (var i=0; i < els.length; i++)
                opts.els.push(els[i]);

			opts.curr = opts.random ? (Math.floor(Math.random() * (els.length-1)))+1 : 1;
			opts.last = 0;
			$els.each(function(i){
				$(this).click(function(){
					$.fn.jqShuffle.go(els, opts);
				});
			});
			
			if(opts.auto)
				this.cycleTimeout = setTimeout(function(){$.fn.jqShuffle.go(els, opts)}, opts.timeout + (opts.delay||0));
		});
	};
	
	$.fn.jqShuffle.go = function(els, opts){
		var last = els[opts.last], curr = els[opts.curr];
		$.fn.jqShuffle[opts.fx](last,curr,opts);
		var roll = (opts.curr + 1) == els.length;
		opts.curr = roll ? 0 : opts.curr+1;
		opts.last = roll ? els.length-1 : opts.curr-1;
		if(opts.auto)
			setTimeout(function() { $.fn.jqShuffle.go(els, opts) }, opts.timeout);
	};

	$.fn.jqShuffle.shuffleLite = function(last, curr, opts, cb) {
		var $el = $(last);
		$el.animate(opts.coords, opts.speed, opts.easing, function() {
			opts.els.push(opts.els.shift());
			for (var i=0, len=opts.els.length; i < len; i++)
				$(opts.els[i]).css('z-index', len-i);
			$el.animate({left:0, top:0}, opts.speedOut, opts.easeOut, cb);
		});
	};

	$.fn.jqShuffle.defaults = {
		fx		: 'shuffleLite',
		speed	: 350,
		auto	: 0,	// true for automated, false for not
		random	: 0,		// true for random, false for sequence
		easing	: null,
		coords	: null,
		width	: 400,
		delay	: 0,     // additional delay (in ms) for first transition (hint: can be negative)
		timeout	: 4000  // ms duration for each slide
	};
})(jQuery);

**/