/************************************************ 
 * Copyright © 2010 Ignition Interactive. All Rights Reserved.  *
 * Programmer: Codin Pangell      			 *
 *                                           *
 * NOTE: Requires jQuery.                    *
 ************************************************
/                                              */
$(document).ready(function() {
	Control.init();
	
})

var Controller = function(){}


Controller.prototype.init=function(){
	var _loc=window.location.toString();
	if (_loc.indexOf("submitted=true")>0){
		alert("Your submission has been made.");
	}
	if (_loc.indexOf("aboutUS.html")>0){
		Control.indexandabout('about');
		MetaControl.init('aboutUS.html');
	}else if (_loc.indexOf("attorneyProfiles.html")>0){
		Control.auxPages("7");
		MetaControl.init('attorneyProfiles.html');
	}else if (_loc.indexOf("contactUS.html")>0){
		Control.contactUs();
		MetaControl.init('contactUS.html');
	}else if (_loc.indexOf("domestic.html")>0){
		Control.auxPages("2");
		MetaControl.init('domestic.html');
	}else if (_loc.indexOf("drugcrimes.html")>0){
		Control.auxPages("1");
		MetaControl.init('drugcrimes.html');
	}else if (_loc.indexOf("dui.html")>0){
		Control.auxPages("3");
		MetaControl.init('dui.html');
	}else if (_loc.indexOf("firmOverview.html")>0){
		Control.auxPages("5");
		MetaControl.init('firmOverview.html');
	}else if (_loc.indexOf("mpd.html")>0){
		Control.auxPages("4");
		MetaControl.init('mpd.html');
	}else if (_loc.indexOf("practiceOverview.html")>0){
		Control.auxPages("6");
		MetaControl.init('practiceOverview.html');
	}else if (_loc.indexOf("resources.html")>0){
		Control.resources();
		MetaControl.init('resources.html');
	}else{ //index page
		Control.indexandabout('index');
		MetaControl.init('index.html');
	}
	
}
Controller.prototype.font=function(){
Cufon.replace('#header .menu a, h2, h3', { fontFamily: 'AlternateGothic2 BT', hover:true });
Cufon.replace('.form-link', { fontFamily: 'Futura BdCn BT', hover:true });
}

Controller.prototype.contactUs=function(){
	$.ajax({ type: "GET",
		url: "json_interface.php?action=contact_form_copy",
		dataType: "json",
		success: function(obj) {
			_xhtml='';
			$.each(obj.data,function(i,item){
				var _tmp=prep(item.contact_form_information).split("<b>").join("<b style='color:#FFFFFF'>");
				_xhtml+='<p>'+_tmp+'</p>';
				_xhtml+='<p><a href="'+prep(item.contact_link)+'" target="_self" class="link">Map &amp; Directions</a></p>';
			});
			$('.column-3').html("<h3>main office</h3>"+_xhtml);
			Control.font();
		}
	});
}
Controller.prototype.resources=function(){
	$.ajax({ type: "GET",
		url: "json_interface.php?action=resources",
		dataType: "json",
		success: function(obj) {
			_xhtml='';
			$.each(obj.data,function(i,item){
				$('#resourceTitle').html(prep(item.title));
				var _tmp=prep(item.column_one).split("<b>").join("<b style='color:#FFFFFF'>");
				$('.col1').append(_tmp);
				_tmp=prep(item.column_two).split("<b>").join("<b style='color:#FFFFFF'>");
				$('.col2').html(_tmp);
				
				_tmp=prep(item.column_three).split("<b>").join("<b style='color:#FFFFFF'>");
				$('.col3').html(_tmp);
				if (item.column_three_image!=""){
					$('.col3').prepend("<h3>MAIN OFFICE</h3><img src='admin/data/uploads/"+item.column_three_image+"' /><br /><br />");
				}
				$('.col3').append('<p><a href="'+prep(item.column_three_large_link)+'" target="_self" class="link">Map &amp; Directions</a></p>');
			});
			$('.right-corner').prepend(_xhtml);
			Control.font();
		}
	});
}
Controller.prototype.auxPages=function(pg){
	$.ajax({ type: "GET",
		url: "json_interface.php?action=auxillary_pages",
		dataType: "json",
		success: function(obj) {
			_xhtml='';
			$.each(obj.data,function(i,item){
				if(pg==item.pagename){	
					var _tmp=prep(item.copy).split("<b>").join("<b style='color:#FFFFFF'>");
					_xhtml+='<h3>'+prep(item.title)+'</h3><p>'+_tmp+'</p>';
					_xhtml+='<p><a href="'+prep(item.large_link)+'" target="_self" class="link">Map &amp; Directions</a></p>';
				}
			});
			$('.right-corner').prepend(_xhtml);
			Control.font();
		}
	});
}
Controller.prototype.indexandabout=function(pg){
	var _xhtml='';
	//calouts
	var tbl='home_callouts';
	if (pg=='about') tbl='about_us_callouts';
	$.ajax({ type: "GET",
		url: "json_interface.php?action="+tbl,
		dataType: "json",
		success: function(obj) {
			$.each(obj.data,function(i,item){
					/*
					if (tbl=='home_callouts'){ //was coded with different styles for some reason
						_xhtml='<h3 class="style4">';
					}else{
						_xhtml='<h3 class="style9">';
					}
					*/
				if (i==0){
					if (tbl=='home_callouts'){ //was coded with different styles for some reason
						_xhtml='<h3 class="style4">'+prep(item.headerTitle)+'</h3><img src="admin/data/uploads/'+prep(item.image)+'" alt="" />';
					}else{
						_xhtml='<h3 class="style9"><span class="style10">'+prep(item.headerTitle)+'</span><img src="admin/data/uploads/'+prep(item.image)+'" alt="" /></h3>';
					}
					_xhtml+='<p class="padding1"><strong>'+prep(item.title)+'</strong></p><p>'+prep(item.short_description)+'</p><a href="'+prep(item.learn_more_link)+'" target="_self" class="link">Learn more</a>';
					$('.abcol1').html(_xhtml);
				}
				if (i==1){
					if (tbl=='home_callouts'){ //was coded with different styles for some reason
						_xhtml='<h3 class="style4">'+prep(item.headerTitle)+'</h3><img src="admin/data/uploads/'+prep(item.image)+'" alt="" />';
					}else{
						_xhtml='<h3 class="style8">'+prep(item.headerTitle)+'<img src="admin/data/uploads/'+prep(item.image)+'" alt="" /></h3>';
					}
					_xhtml+='<p class="padding1"><strong>'+prep(item.title)+'</strong></p><p>'+prep(item.short_description)+'</p><a href="'+prep(item.learn_more_link)+'" target="_self" class="link">Learn more</a>';
					$('.abcol2').html(_xhtml);
				}
				if (i==2){
					if (tbl=='home_callouts'){ //was coded with different styles for some reason
						_xhtml='<h3 class="style4">'+prep(item.headerTitle)+'</h3><img src="admin/data/uploads/'+prep(item.image)+'" alt="" />';
					}else{
						_xhtml='<h3 class="style9"><strong>'+prep(item.headerTitle)+'</strong><img src="admin/data/uploads/'+prep(item.image)+'" alt="" /></h3>';
					}
					_xhtml+='<p class="padding1"><strong>'+prep(item.title)+'</strong></p><p>'+prep(item.short_description)+'</p><a href="'+prep(item.learn_more_link)+'" target="_self" class="link">Learn more</a>';
					$('.abcol3').html(_xhtml);
				}
			});
			Control.font();
		}
	});
	//copy sections
	var tbl2='home_copy_sections';
	if (pg=='about') tbl2='about_us_copy_sections';
	$.ajax({ type: "GET",
		url: "json_interface.php?action="+tbl2,
		dataType: "json",
		success: function(obj) {
			_xhtml='';
			$.each(obj.data,function(i,item){
				_xhtml+='<h3>'+prep(item.title)+'</h3>';
				if (item.optional_image!=""){
					_xhtml+='<div class="container">';
					_xhtml+='<img src="admin/data/uploads/'+item.optional_image+'" class="imgindent" alt="" /></div>';
				}
				var tmp=prep(item.description);
				tmp=tmp.split("<b>").join("<span class='style10'>");
				tmp=tmp.split("</b>").join("</span>");
				_xhtml+='<p>'+tmp+'</p>';
				if (item.largeLink!=""){
					_xhtml+='<a href="'+prep(item.largeLink)+'" target="_self" class="link">Map &amp; Directions</a>';
				}
			});
			$('.abcontent1').html(_xhtml);
			Control.font();
		}
	});
	//contact form
	$.ajax({ type: "GET",
		url: "json_interface.php?action=contact_form_copy",
		dataType: "json",
		success: function(obj) {
			_xhtml='';
			$.each(obj.data,function(i,item){
				var info=prep(item.contact_form_information);
				_xhtml+='<p class="style1">'+prep(item.contact_form_information)+'</p>';
			});
			$('.abcontent2').prepend(_xhtml);
			Control.font();
		}
	});
}
var Control = new Controller();
function prep(str){ //before redisplay
	if (str==undefined)	return "";
	str=str.split("[lbr]").join("<br />");
	str=str.split("[amp]").join("&");
	str=str.split("[ques]").join("?");
	str=str.split("[eql]").join("=");
	str=str.split("[pnd]").join("#");
	str=str.split("[apos]").join("'");
	str=str.split("[dpos]").join('"');
	return str;
}
