/* Web Defined
*  071410
*/


/** VARS **********************************************************************/
var disOpen = new Boolean();
var fadeSpeed;
var offset;
var scrolltop;
var bread;
var bread_search = 'http://www.webdefined.us/';

disOpen = false;
fadeSpeed  = "fast";



/** ON READY **********************************************************************/
$().ready(function() {	
	bread = $('.breadcrumb').text();
	
	/** Events **************************************************/
	//contact link
	$('.email').click(function() {
	  	offset = $(this).offset();
		scrolltop = $(window).scrollTop();
		callData('form','');
		if(window.event) event.returnValue = false;
		return false;
	});
	
	$('div#dialog-content form').submit(function() {
		return false;
	});

	//portfolio
	$('.project_hoverPhoto').click(function() {
		var id = $(this).attr('id');
	  	offset = $(this).offset();
		scrolltop = $(window).scrollTop();
		callData('port',id);
		if(window.event) event.returnValue = false;
		return false;
	});
	
	$('.textLink').click(function() {
		var id = $(this).attr('id');
	  	offset = $(this).offset();
		scrolltop = $(window).scrollTop();
		callData('port',id);
		if(window.event) event.returnValue = false;
		return false;
	});
		
//dialog - close
	$('div#dialog .close').click(function () { //hide video player
		dialogClose();
		if(window.event) event.returnValue = false;
		return false;
	});
	
	$('.bgCover').click(function () { //hide video player
		dialogClose();
		if(window.event) event.returnValue = false;
		return false;
	});
	
	//search form
	$('form#search input#txt').click(function() {
		$('form#search input#txt').val('');
		if(window.event) event.returnValue = false;
		return false;
	});

});


/** DIALOG *************************************************************************/
function showOverlayBox() {
	if( disOpen == false ) return;  //if  not set to open -> do nothing
	$('#dialog').css({display:"block",left:( $(window).width() - $('#dialog').width() )/2,top:scrolltop,position:"absolute"});	
	$('.bgCover').css({width: $(window).width(),height:$('#wrap').height() + 40}); 
}

function doOverlayOpen() {
	disOpen = true; 
	showOverlayBox(); 
	$('.bgCover').fadeIn(fadeSpeed);
	return false; 
}

function doOverlayClose(){
	disOpen = false;
	$('.bgCover').css({opacity:0.70});
}

function dialogClose(){
	 $('#dialog').hide(1, function () {
		isOpen = false;
		$('.bgCover').fadeTo(400,0, function () {
			$('.bgCover').hide();
			doOverlayClose();
		});
	});
	
}

function openRSDialog(){
	$('.bgCover').css({opacity:0.70}); 
	$(window).bind('resize',showOverlayBox); 
	doOverlayOpen();
} 


/** DATA *************************************************************************/
function callData(type, fid){
	var furl;
	if(type == 'form'){ 
		if(bread == 'Search Results'){ furl = '../inc/contact.php'; }
		else{ furl = 'lib/inc/contact.php'; } 
		$('#dialog').addClass('dialog-form');
	}
	if(type == 'port'){ 
		furl = 'lib/inc/portfolio.php'; 
		$('#dialog').addClass('dialog-port');
	}
	$.ajax({
		url: furl
		,cache: false
	   ,type: "POST"
		,data: ({
			id: fid
		})
	   ,dataType: "html"
	   ,error: function(XMLHttpRequest, textStatus, errorThrown){
		// not
	  }
	   ,success: function(data){
			$('div#dialog-content').empty();
			openRSDialog();
			$('div#dialog-content').html(data);
			if(type == 'form'){ 
				$('div#dialog-content form').validate({  //lazy init -> call here
					submitHandler: function(form) {
						submitForm();
					 }
				});
			}
			
		} //eo success
	 }); //eo ajax
}//eo callData

function submitForm(){
	var furl;
	var serializedData = wrapFormValues('form#pop-form');
	if(bread == 'Search Results'){ furl = '../inc/email.php'; }
	else{ furl = 'lib/inc/email.php'; } 
	
	$.ajax({
		url: furl
		,cache: false
	   ,type: "POST"
		,data: ({
			submitdata: serializedData
		})
	   ,dataType: "text"
	   ,error: function(XMLHttpRequest, textStatus, errorThrown){
			 alert('The connection was lost. Please try again.');
		  }
	   ,success: function(text, textStatus){
			if(textStatus == 'success'){
				$('div#dialog-content div#subtitle').css('margin-bottom', '5px');
				$('div#dialog-content input.button').css('display', 'none');
				$('div#dialog-content div#thanks').html('Thank you, your email has been sent.');
			}
		}
	 }); //eo ajax
}

function wrapFormValues(form){ 
    form = $(form).serializeArray();
    var dataArray = new Object();
    for(index in form){   
    	dataArray[form[index].name] = form[index].value;        
    }           
    return dataArray; 
}
