//For all browser detablify
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "/mobile/deTable.css");
//Append to end of head
document.getElementsByTagName("head")[0].appendChild(fileref);
////////////////////////////////////////////
var G_mobileBrowswer = 0;		//Default to non mobile browser
////////////////////////////////////////////
//Get User Agent and convert to lowercase
var agent=navigator.userAgent.toLowerCase();
/*
var is_firefox = (agent.indexOf('firefox')!=-1);
if (is_firefox) { G_mobileBrowswer = 1; }
*/
var is_deviceIphone = (agent.indexOf('iphone')!=-1);
if (is_deviceIphone) { G_mobileBrowswer = 1; }
var is_deviceIpod = (agent.indexOf('ipod')!=-1);
if (is_deviceIpod) { G_mobileBrowswer = 1; }
var is_deviceIpod = (agent.indexOf('ipad')!=-1);
if (is_deviceIpod) { G_mobileBrowswer = 1; }
var is_deviceAndroid = (agent.indexOf('android')!=-1);
if (is_deviceAndroid) { G_mobileBrowswer = 1; }
var is_deviceWinMob = (agent.indexOf('windows ce')!=-1);
if (is_deviceWinMob) { G_mobileBrowswer = 1; }
var is_deviceBB = (agent.indexOf('blackberry')!=-1);
if (is_deviceBB) { G_mobileBrowswer = 1; } 
var is_devicePalm = (agent.indexOf('palm')!=-1);
if (is_devicePalm) { G_mobileBrowswer = 1; } 
 
if (G_mobileBrowswer == 1) {
	var G_viewNormal = 0;			//Default to mobile view if device is mobile
	checkViewCookie(); 					//Check if viewNormal cookie set
	if (G_viewNormal == 0) {
		////////////////////////////////////////////
		//Redirect if not on residential or commercial
		var url = document.location.href;
		
		var is_residential = (url.indexOf('/SearchApartments')!=-1);
		var is_residentialDetail = (url.indexOf('Residential')!=-1);
		var is_commercial = (url.indexOf('Commercial')!=-1);
		var redirect = 1;
		
		if ( is_residential || is_residentialDetail || is_commercial){
			redirect = 0
		}
		if (redirect==1) {
			window.location = "http://www.rentcip.com/SearchApartments.aspx";
		}
		////////////////////////////////////////////
		//Apply mobile.css
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", "/mobile/mobile.css");
		//Append to end of head
		document.getElementsByTagName("head")[0].appendChild(fileref);
			
		if (is_residentialDetail){
			////////////////////////////////////////////
			//Apply details.css
			var fileref=document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", "/mobile/details.css");
			//Append to end of head
			document.getElementsByTagName("head")[0].appendChild(fileref);	
		}		
	}
}
 
$(document).ready(function() {
	if (is_residentialDetail || is_commercial){
		//Set background while
		$(".right").css("background-color","white");
		//Insert Tabs
		var tabsHTML = '<ul id="searchTabs" style="padding-left: 0px; margin-bottom: 0px;">';
		tabsHTML = tabsHTML+'<li style="float: left; margin: 0px 0px 0px 10px; padding: 0pt; list-style: none outside none;"><a href="http://www.rentcip.com/SearchApartments.aspx" class="residential">Apartments</a></li>';
		tabsHTML = tabsHTML+'<li style="float: left; margin: 0px 0px 0px 10px; padding: 0pt; list-style: none outside none;"><a href="http://www.rentcip.com/Commercial.aspx" class="commercial">Commercial</a></li>';
		tabsHTML = tabsHTML+'<div class="clearfix" style="clear:both;"></div>';
		tabsHTML = tabsHTML+'</ul>';
		$("#Body").prepend(tabsHTML);
		
		$(".residential").css("background","-webkit-gradient(linear, 0% 0%, 0% 100%, from(#910009), to(#B9000B))");
		$(".residential").css("background","-moz-linear-gradient(100% 100% 90deg, #910009, #B9000B)");
		//Insert Footer
		var footerHTML = '<div id="mobileFooter">';
		footerHTML = footerHTML+'<input id="searchAgainRes" type="button" class="button sa" onClick="window.location=\'http://www.rentcip.com/SearchApartments.aspx\';" value="Back to Search" />';
		footerHTML = footerHTML+'<div class="extras">';
		footerHTML = footerHTML+'';
		footerHTML = footerHTML+'</div>';
		footerHTML = footerHTML+'<!-- real estate logos -->';
		footerHTML = footerHTML+'<div class="clearfix" style="clear:both;"></div>';
		footerHTML = footerHTML+'<img src="/mobile/img/icons.gif" style="padding:3px;">';
		footerHTML = footerHTML+'<p><a href="#" onClick="viewNormal(); return false;" class="viewfullsite">View Full Site</a></p>';
		footerHTML = footerHTML+'</div></div>';	
		$("#Body").append(footerHTML);
	
	}
	//Mobile
	if (G_viewNormal == 0) {
		//Search Button Show?
		var G_searchOn = 1;			//Start by showing Search Button
		checkSearchCookie();
		if (G_searchOn==0) {
			//document.getElementById('search').style.display = 'none';
			//document.getElementById('goFilterMobile').style.display = 'none';
			//document.getElementById('searchAgainRes').style.display = 'block';
		}
	}
	else {
		if (G_mobileBrowswer == 1) {
			var mobileLinkHTML = '<div style="text-align:center;margin:10px;" id="mobileLink"><a style="text-decoration:none;color:white;font-size:21px;" href="http://www.rentcip.com/SearchApartments.aspx" onClick="deleteCookie( \'viewNormal\', \'/\', \'\' );">Mobile Version</a></div>';
			$("#footer-wrapper").prepend(mobileLinkHTML);
		}
	}
}); 
/////////////////////////////////////////////////////////////
function submitResidential(){
	if(G_mobileBrowswer == 1){
		setCookie('searchOn','no',100);
		G_searchOn = 0;
		//document.getElementById('search').style.display = 'none';
		//document.getElementById('goFilterMobile').style.display = 'none';
		//document.getElementById('searchAgainRes').style.display = 'block';
		
		window.location.hash="firstResult";
	}
} 
function searchAgain() {
	//document.getElementById('search').style.display = 'block';
	//document.getElementById('goFilterMobile').style.display = 'block';
	setCookie('searchOn','yes',100);
	G_searchOn = 1;
	
	window.location.hash="top";
}
function viewNormal(){
	setCookie('viewNormal','1',100);
	location.reload(true);
	//window.location = "http://www.rentcip.com";	
}
/////////////////////COOKIES///////////////////////////////
function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + ";path=/";
	document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
		return unescape(y);
		}
	}
}
function checkViewCookie(){
	var viewNormal=getCookie("viewNormal");
	if (viewNormal!=null && viewNormal!="")	{G_viewNormal = 1;}
}
// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



function checkSearchCookie(){
	var searchState=getCookie("searchOn");
	if (searchState=="no")
	{
		//document.getElementById('search').style.display = 'none';
		//document.getElementById('goFilterMobile').style.display = 'none';
		//document.getElementById('searchAgainRes').style.display = 'block';
	}
}

