insertDealerVPU();

$(document).ready(function(){
	insertHeadLogoFlash();
	insertMenu();
})

function insertDealerVPU(){
	if(checkDealer()) return;
	
	document.write('<script type="text/javascript" src="virtualpopup.js"></script>');
  	document.write('<link href="virtualpopup.css" type="text/css" rel="stylesheet">');
}

function insertHeadLogoFlash(){
	swfobject.embedSWF("flash/headlogo.swf?version=20111128", "headerlogo", "936", "115", "8.0.0", "flash/expressInstall.swf", flashvars, params, null);
}

function insertIndexFlash(){
	var tmp = $("#indexanimation a");
	for(i=0;i<tmp.length; i++){
		add(tmp[i].href,tmp[i].title,$(tmp[i]).find("img")[0].title,$(tmp[i]).find("img")[0].alt);
	}
	swfobject.embedSWF("flash/indexanimation.swf", "indexanimation", "936", "380", "10.0.0", "flash/expressInstall.swf", getData(), params, null);
}

function insertShowRoomFlash(){
	var tmp = $("#showroomanimation a");
	for(i=0;i<tmp.length; i++){
		add(tmp[i].href,tmp[i].title,$(tmp[i]).find("img")[0].title,$(tmp[i]).find("img")[0].alt);
	}
	swfobject.embedSWF("flash/showroomanimation.swf?version=20111119", "showroomanimation", "936", "487", "10.0.0", "flash/expressInstall.swf", getData(), params, null);
}

function insertMenu(){

	$("#menu a").mouseenter(function(){
		if(!$(this).hasClass("smparent")) subMenu(false)
		else subMenu(true);
	})

	swfobject.embedSWF("flash/submenu.swf?version=20111119", "submenuflash", "165", "165", "8.0.0", "flash/expressInstall.swf", null, params, null);
	var tmp = $("#wrapper").height();
	if(tmp < 785) tmp = 785; 
	swfobject.embedSWF(getMenu()+"?version=20111124", "menu", "54", tmp, "8.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
}

function closeSubMenu(){
	if($("#submenuflash").find("a").html() == null) subMenu(false);
}

function subMenu(flag){
	if(flag){
		$("#submenu").css('display','block');
	} else {
		$("#submenu").css('display','none');
	};
}

function getMenu(){
	if(checkDealer()) return "flash/menu_nd.swf"
	else return "flash/menu.swf";
}

function checkDealer(){
	CheckSetDealerCode();
	if(Get_Cookie(dcCookie) || !CheckCookiesEnabled()) return true
	else return false;
}


function gotologo(id) {
	if(id == 1) window.open('green_'+'certification.'+'html');	
	if(id == 2) window.open('http'+'://'+'www.'+'rptia.'+'org');	
	if(id == 3) window.open('http'+'://'+'www.'+'crva.'+'ca');
	if(id == 4) window.open('http'+'://'+'www.'+'qai.'+'org');
}


function gotologo1(id) {
	if(id == 1) location.href='green_'+'certification.'+'html';	
	if(id == 2) location.href='http'+'://'+'www.'+'rptia.'+'org';	
	if(id == 3) location.href='http'+'://'+'www.'+'crva.'+'ca';
	if(id == 4) location.href='http'+'://'+'www.'+'qai.'+'org';
}

var _data = new Array();
var _tmpvars;

function add(_img,_desc,_time,_url){
		var _tmpvars = {
			img: _img,
			desc: _desc,
			time: _time,
			url: _url
		};
	_data.push(_tmpvars);
}

//console.log(_data[0].img+" "+_data[0].desc+" "+_data[0].time+" "+_data[0].url);

function getData(){
	if(_data.length == 0) return null;
	var _xml = "<xml>";
	for(i = 0; i < _data.length; i++){
		_xml += "<item><img>"+_data[i].img+"</img>";
		_xml += "<desc>"+_data[i].desc+"</desc>";
		_xml += "<time>"+_data[i].time+"</time>";
		_xml += "<url>"+_data[i].url+"</url></item>";
	}
	_xml+="</xml>";
	return {xml: _xml};
}


//floorplans

var tmpFP;
var tmpFPpath;

function viewoption(obj,opt){
	tmpFP = $(obj).parent().parent().parent().parent();
	tmpFPpath = $(tmpFP).find('img').attr('src');
	$(tmpFP).find('img').css('background',"url("+tmpFPpath+")");
	$(tmpFP).find('img').attr('width',$(tmpFP).find('img').attr('width'));
	$(tmpFP).find('img').attr('height',$(tmpFP).find('img').attr('height'));
	$(tmpFP).find('img').attr('src','images/dot.gif');
	$(tmpFP).find('img').attr('src',tmpFPpath.split('.')[0]+'_opt'+opt+'.png');
}

function viewbase(){
	$(tmpFP).find('img').attr('src',tmpFPpath);
	$(tmpFP).find('img').css('background',"none");
}


function floorpageready(){

	$.ajax({
   	type: "GET",
		url: "msrp.xml",
		dataType: "xml",
		success: function(xml) {
 			var tmp = $(xml).find("model");
 			for(var i=0; i < tmp.length; i++){
 				$("#"+$(tmp[i]).attr('id')).html("$ "+prepareMSRP($(tmp[i]).attr('msrp'))+" starting MSRP*");
 			}
		}
	});


	var tmp = $('#content .floorplan')
	$(tmp).eq(tmp.length-1).css('borderBottom','none');
	$(tmp).eq(tmp.length-1).css('paddingRight','0px');
	$(tmp).eq(tmp.length-1).css('width','437px');
		console.log('1');
	if(tmp.length%2==0){
		console.log('2');
		$(tmp).eq(tmp.length-2).css('borderBottom','none');
	} else {
		console.log('3');
		$(tmp).eq(tmp.length-1).find('p').css('position','absolute');
		$(tmp).eq(tmp.length-1).find('p').css('marginLeft','457px');
	}
	
		console.log('4');
	for(var k=0; k < tmp.length; k++){
		if(k%2==1) $(tmp).eq(k).css('paddingLeft','20px');
	}	
		console.log('5');
	
}

function prepareMSRP(tmp){
	var tt = '';
	for(var j=tmp.length; j>0;j--){
		tt+=tmp[j-1];
		if(j%3 == 0) tt+=',';
	}
	tmp = ''
	for(var j=tt.length; j>0;j--){
		tmp+=tt[j-1];
	}
	return tmp;
}

// construction

function constructionpageready(){
	
	checkSize();
	
	var string = '<woo>';

 	string+='<firstitem><title><![CDATA['+ $('#firstitem h2').eq(0).html() +']]></title>';
 	string+="<description><![CDATA[<FONT SIZE='16' COLOR='#5e8118'>"+ $('#firstitem h3').eq(0).html()+'</FONT>';
 	string+='<br>'+$('#firstitem h4').eq(0).html()+']]></description></firstitem>'
 
 	var items = $('#construction div[name="item"]');
	
 	for(var i = 0; i < items.length; i++)
 	{
 		string+='<item';
 		 		
 		if($(items).eq(i).find('span').length > 0)
 			string+=" type='"+$(items).eq(i).find('span').eq(0).html()+"'";
 		
 		
 		string+='><img><![CDATA['+$(items).eq(i).find('i').eq(0).html()+']]></img>';
 		
		var pp = $(items).eq(i).find('strong');
 		for(var j = 0; j < pp.length; j++)
 		{
 			string+='<feature>';
 			if($(pp).eq(j).find('p').length > 0)
 				string+='<schema><![CDATA['+ $(pp).eq(j).find('p').eq(0).html() +']]></schema>';
 			string+='<name><![CDATA['+ $(pp).eq(j).find('h1').eq(0).html() +']]></name>';
 			string+='<title><![CDATA['+ $(pp).eq(j).find('h2').eq(0).html() +']]></title>';
 			string+="<description><![CDATA[<FONT SIZE='16' COLOR='#5e8118'>"+ $(pp).eq(j).find('h3').eq(0).html()+'</FONT>';
 			string+='<br>'+$(pp).eq(j).find('h4').eq(0).html()+']]></description>';
 			string+='</feature>';
 		}
 		string+='</item>';
		
 	}
 
 	string += '</woo>';

 	var www = new Array();
 	www = string.split('&amp;');
 	string = www.join('%26');
  	www = string.split('"');
 	string = www.join('%22');	
	swfobject.embedSWF("flash/constructionanimation.swf?version=20111201", "construction", "936", "625", "10.0.0", "flash/expressInstall.swf", {xml: string}, params, null);

}

function checkSize(){
	if(getClientHeight()>750)if(getClientWidth()>1000) return;
	alert('This page requires larger browser viewport for optimal view. You can try switching to fullscreen mode by pressing F11 or maximizing the browser window for better results.');
}

// cookies-related functions

function GetArgument(name){
	var ss=unescape(window.location.search.substring(1));
	var re=new RegExp(name+"=([^&]*)");
	var ar=ss.match(re);
	if(ar) return ar[1]; else return null;
}

function Set_Cookie( name, value, expires, path, domain, secure ){
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  // expires is specified in days
	// if expires=='' cookie will only last as long as the current session of the visitor

  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires ){
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ){
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){
		return null;
	}
}

function Delete_Cookie( name, path, domain ){
  if ( Get_Cookie( name ) ) document.cookie = name + "=" +
  ( ( path ) ? ";path=" + path : "") +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

var cookiesEnabled=false;

function CheckCookiesEnabled(){
	cookiesEnabled=false;
	var cName='TestCookiesEnabled';
	Set_Cookie(cName, 'none', '', '/', '', '');
	if(Get_Cookie(cName))
	{
		Delete_Cookie(cName, '/', '');
		cookiesEnabled=true;
	}
	return cookiesEnabled;
}

// dealer code checking
var dcCookie='DealerCode';

function CheckSetDealerCode(){
	var code=GetArgument('code');
	if(code) Set_Cookie(dcCookie, code, '', '/', '', '');
}

function DealerLocatorDiv(){
	CheckSetDealerCode();
	if(Get_Cookie(dcCookie) || !CheckCookiesEnabled()) document.write('<div style="display: none;">');
	else document.write('<div>');
}

function DealerLocatorP(){
	CheckSetDealerCode();
	if(Get_Cookie(dcCookie) || !CheckCookiesEnabled()) document.write('<p class="li" style="display: none;">');
	else document.write('<p class="li">');
}

//common

function getClientHeight(){
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getClientWidth(){
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

