/*
Object inheritance framework from
http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html

Example implementation:

function Foo (a, b, c) {
	// do something
}

Bar.Inherits(Foo);
function Bar (a, b) {
	var c = 'blah';
	this.Inherits(Foo, a, b, c);
}

*/

/* Object.prototype.Inherits = function( parent )
	{
		if( arguments.length > 1 )
		{
			parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
		}
		else
		{
			parent.call( this );
		}
	}
*/

Function.prototype.Inherits = function( parent )
	{
		this.prototype = new parent();
		this.prototype.constructor = this;
	}

  // **************************************************************************************************
  // ****      I.E. DOM IMPLEMENTATION ISSUE FIX                                                   ****
  // **************************************************************************************************

  // Stub function
  function createElementWithName(){}

  // Anonymous function fills in stub automatically depending on browser implementation of name
  // attribute.  IE requires a non-standard call to document.createElement in order to set the name
  // attribute for elements, which is necessary for dynamically generated forms.
  (function(){
    try {
      var el=document.createElement( '<div name="foo">' );
      if( 'DIV'!=el.tagName ||
          'foo'!=el.name ){
        throw 'create element error';
      }
      createElementWithName = function( tag, name ){
        return document.createElement( '<'+tag+' name="'+name+'"></'+tag+'>' );
      }
    }catch( e ){
      createElementWithName = function( tag, name ){
        var el = document.createElement( tag );
        // setAttribute might be better here ?
        el.name = name;
		el.setAttribute("name",name);
        return el;
      }
    }
  })();

  // Add createElementWithName to document object as createNamedElement
  if (typeof(document.createNamedElement) == 'undefined') {
    document.createNamedElement = createElementWithName;
  }

  /*
  Example usage:
	// Declare variables object
	var flashvars = {
		handle:'handletest'
		,flashVarText:'testing'
		,anothervar:'testing'
	};
	// Embed Flash object in page
	var so = new swfoWrapper('so_tester.swf','flashcontent',flashvars,300,300);
  */
  swfoWrapper.Inherits(SWFObject);




  //var swfoCoreURL = (swfoBaseURL.length > 0 ? window.location.protocol+"//"+swfoBaseURL : "");
  //var swfoCoreURL = (swfoBaseURL.length > 0 ? window.location.protocol+"//"+swfoBaseURL : "") + "/flash/core.swf";

  //var swfoCoreURL = swfoBaseURL.replace(/https:\/\//i,"http://") + "/flash/core.swf";


  function swfoWrapper (swf, container, vartable, width, height){
	// this = new SWFObject();
	// default required flash version, background color
	this.requiredversion = 8;
	this.backcolor = '#ffffff';
	// if width and height specified then set, otherwise default
	this.height = !isNaN(width) ? width : 964;
	this.width = !isNaN(height) ? height : 555;
	// parameters provided by default constructor
	this.swf = (/^http/).test(swf) ? swf :  swfoBaseURL+swf  ;
	this.container = container;
	// call the SWFObject constructor
	this.Inherits(SWFObject, this.swf, container + '_swf', this.height, this.width, this.requiredversion, this.backcolor);

	// add all variables!
  	//this.setAttribute('redirectUrl', 'http://www.nissanusa.com/global/error/getflash.html');

  	//Changes made by : Chandan Lakkoju -- Start here
  	//this.setAttribute('redirectUrl', 'http://www.nissanusa.com/global/error/getflash.html');

  	this.setAttribute('redirectUrl', '/non-flash/index.html');
  	//Changes made by : Chandan Lakkoju -- End here
	if(isSpanishPage){
		this.addVariable( "lang",  "es" );
	}

	if(typeof crmPageId!="undefined"){
		this.addVariable("pageid", crmPageId);
	}

	//Add some variables and params from Refresh2
	this.addVariable( "handle",  location.pathname.replace(/\.(x|ht)ml$/,'')  );

	//-var basePrefix=(typeof nissanRootSrc == 'string' )?nissanRootSrc:'http://www.nissanusa.com';
	//-this.addParam('base',basePrefix+'/flash');

	this.addParam('base',swfoBaseURL+'/flash');
	//this.addParam('base',swfoBaseURL);

	//this.addVariable('url',window.location.protocol+"//"+window.location.host+window.location.pathname);

	this.addParam('scale','noscale');
  	// Added for SR: 12472
	//this.addParam('wmode','transparent');
  	// Ends here SR: 12472
	this.addParam('allowScriptAccess','always');


	/// begin regional nav text.
	this.doRegionalNav=function(){
		var _lp=location.pathname.replace(/\/$/,'/index.html');//location pathname
		var _pnp=location.pathname.replace(/^\/espanol/,'').split("/");//pathname parts
		_pnp.shift();// shift off the always-blank first part
		_pnp[_pnp.length-1]=_pnp[_pnp.length-1].replace('index.html','');// if it's index, zap it to simplify.
		//_pnp=Array.concat(_pnp,['','','']);//add a few blanks
		_pnp.push('');
		_pnp.push('');
		_pnp.push('');
		switch(_pnp[0]){
			case 'about': /// about is pretty easy
				if(!_pnp[1]){return false}//blank. index.
				return _pnp[1];
			case 'buying': /// buying. go from most specific to most general to reduce number of conditionals
				if(!_pnp[1]){return false}//blank. index.
				if(_pnp[2]=='faqs'){return '/buying/nissan-financing/faqs/'} //credit.html
				if(_pnp[2]=='current-offers.html'){return '/buying/nissan-financing/current-offers.html'}
				if(_pnp[1]=='finance-account-manager.html'){return '/buying/finance-account-manager.html'}
				if(_pnp[1]=='nissan-financing'){return '/buying/nissan-financing/index.html'}
			case '':;
		}
		if(isVehiclePage && /^(specifications|packaged-options|individual-options|accessories)/.test(_pnp[1]) ){
			return 'specifications';
		}
		if(isVehiclePage && /^(awards)/.test(_pnp[1]) ){
			return 'awards';
		}
		return false;
	}
	if(height>width){
		var regNavUrlPart=this.doRegionalNav();
		//console.info(regNavUrlPart)
		if(regNavUrlPart){
			//console.info(regNavUrlPart)
			this.addVariable( "regNavUrlPart", regNavUrlPart );
		}
	}
	/// end regional nav text.




	for (var varitem in vartable) {
		if (typeof(vartable[varitem]) != 'function'){
			if(/^atr_/.test(varitem)){
				this.setAttribute(varitem.replace(/^atr_/,''),vartable[varitem]);
			}else if(/^param_/.test(varitem)){
				this.addParam(varitem.replace(/^param_/,''),vartable[varitem]);
			}else{
				this.addVariable(varitem,String(vartable[varitem]).replace(/&amp;/g,'%26').replace(/\+/g,'%2B'));
			}
		}
	}
	if(vartable.CFE && vartable.CFE=='true'){
		//this.addVariable('url',location.protocol+"//"+location.host+location.pathname+  ((location.port) ? ":"+location.port : "")  );

    // keep hash in for flash
		//this.addVariable('url',location.href.replace(/#.+/,'').replace('?','%3F').replace(/&/g,'%26').replace(/=/g,'%3F')  );
		this.addVariable('url',location.href.replace('?','%3F').replace(/&/g,'%26').replace(/=/g,'%3F').replace('dhash%3F','#')  );

		this.addParam('wmode','transparent');
	}
	if(vartable.page=='globalnav'){
		this.addParam('wmode','transparent');
	}
	// so flash has its own id:
	this.addVariable( "htmlSwfId",  vartable['atr_id']||container + '_swf' );
	///this.addVariable('url',""); //#  TEST




	this.postform = function (action,query) {
		var fe = document.createElement("form");
		fe.id = "flashsearchform";
		fe.action = action;
		fe.enctype = "multipart/form-data";
		fe.encoding = "multipart/form-data";
		fe.method = "post";
		fe.style.display = "none";
		var qv = "";
		var qa = query.split(",");
		for (var i = 0; i < qa.length; i++) {
			qv += String.fromCharCode(qa[i])
		}

		var qps = qv.split("&");
		for (var i = 0; i < qps.length; i++) {
			var qp = qps[i].split("=");
			var qpe = document.createNamedElement("input",qp[0]);
			qpe.type = "hidden";
			var qpev = qp[1];
			qpe.value = qpev;
			fe.appendChild(qpe);
		}

		var qpcs = document.createNamedElement("input","_charset_");
		qpcs.type = "hidden";
		qpcs.value = "utf-8";
		fe.appendChild(qpcs);
		document.body.appendChild(fe);
		fe.submit();
	}

	this.write(this.container);
	return;
  }

  swfoWrapper.prototype.Inherits=function(parent){
	if( arguments.length > 1 ){
		parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
	}else{
		parent.call( this );
	}
}