// utility --------------------------------------------

var BASEURI = "/car";
var MAINWINDOW_WIDTH = 1000;
var MAINWINDOW_HEIGHT = 700;
var POPUPWINDOW_LEFT = 310;
var POPUPWINDOW_TOP = 95;

// onload

window.onload = function()
{
	var flash = document.getElementById("flash");
	if (flash)
	{
		var wHeight = getWindowSize().height;
		if (wHeight<MAINWINDOW_HEIGHT)
		{
			flash.style.marginTop = Math.ceil(-(MAINWINDOW_HEIGHT-wHeight)/2) + "px";
		}
	}
}

function launchWebsite(u)
{
	if (!u)
		return;

	var ATTRIBUTES = "location=no,menubar=no,resizable=no,scrollbars=no,status=no";
	
	var left = (screen.width-MAINWINDOW_WIDTH)/2;
	var top = (screen.height-MAINWINDOW_HEIGHT)/2;

	var target = u+"";
	if ( target.indexOf("fullscreen=true")==-1 )
		target = target + ( target.indexOf("?")!=-1 ? "&" : "?" ) + "fullscreen=true";
		
	var w = window.open(target,
							"hondaautowebsite",
							"width=" + MAINWINDOW_WIDTH + ",height=" + MAINWINDOW_HEIGHT + ",left=" + left + ",top=" + top + "," + ATTRIBUTES);
							
	if ( w )
		w.focus();
}

function getWindowPosition()
{
	var p = new Object();
	p.x = document.all ? window.screenLeft + 0 : window.screenX + 0;
	p.y = document.all ? window.screenTop + 0 : window.screenY + 0;
	return p;
}

function getWindowSize() 
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {"width":myWidth, "height":myHeight}
}

var detpop = null;

function openDetailPopup(uri, width, height, center )
{
	var props = "width=" + width +
				",height=" + height +
				",top=" + (getWindowPosition().y + ( center ? ( ( MAINWINDOW_HEIGHT - height ) / 2 ) : POPUPWINDOW_TOP ) ) +
				",left=" + (getWindowPosition().x + ( center ?  ( ( MAINWINDOW_WIDTH - width ) / 2 ) : POPUPWINDOW_LEFT ) ) +
				",location=no,menubar=no,resizable=no,scrollbars=no,status=no";
	detpop = window.open(uri, "", props);
}

// flash callback -------------------------------------

/**
 * set bookmark
 */
function setBookmark(uri)
{
	var title = "Honda";
	var url = "http://" + document.domain + uri;	
	url = url.split(".xml").join(".fsp");
	
	if ( url.indexOf(".fsp")!=-1 )
	{
		if (document.all)
			window.external.AddFavorite(url, title);
		else if (window.sidebar)
			window.sidebar.addPanel(title, url, "");
	}
}

/**
 * track
 */
function track(uri)
{
	trackUri(uri);
	var turi = uri.split(".xml").join(".fsp");
	/*
	if (document.location.href.indexOf(turi)==-1 && turi.indexOf("car/content/index.fsp")==-1)
		document.location = "#" + turi;
	*/
}

/**
 * track
 */
function printPage(uri)
{
	var u = uri.split(".xml").join(".fsp");
	if ( u.indexOf("?")==-1 )
		u = u + "?";
	else
		u = u + "&";
	u = u + "print=true";
	window.open(u);
}

/**
 * gotoHref
 */
function gotoHref(uri)
{
	if ( uri.indexOf(".jpg")!=-1 ||
			uri.indexOf(".gif")!=-1 ||
			uri.indexOf(".png")!=-1 )
		gotoHrefImage(uri);
	else if ( uri.indexOf(".flv")!=-1 )
		gotoHrefFlv(uri);
	else if ( uri.indexOf(".mov")!=-1 )
		gotoHrefQuicktime(uri);
	else if ( uri.indexOf(".zip")!=-1 || uri.indexOf(".sit")!=-1 )
		gotoHrefDownload(uri);
	else if ( uri.indexOf("/res/extra/3d/")!=-1 )
		gotoHref3D(uri);
	else if ( uri.indexOf("dealermap.jsp")!=-1 )
		gotoHrefDealerMap(uri);	
	else if ( uri.indexOf("configurator/sendfriend.jsp")!=-1 )
		gotoHrefConfiguratorSendFriend(uri)
	else if ( uri.indexOf("profile/rememberpassword.jsp")!=-1 )
		gotoHrefProfileRememberPassword(uri)
	else
		window.open(uri, "", "location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes");

	trackUri(uri);
}

// resource handling -------------------------------------

function gotoHrefImage(uri)
{
	openDetailPopup( BASEURI + "/view/popup/image.html?" + uri, 10, 10, false );
}

function openFLV(uri)
{
	gotoHrefFlv(uri);
}

function gotoHrefFlv(uri)
{
	openDetailPopup( BASEURI + "/view/popup/flvplayer.html?" + uri, 320, 280, true );
}

function gotoHrefQuicktime(uri)
{
	openDetailPopup( BASEURI + "/view/popup/quicktimeplayer.html?" + uri, 400, 300, true );
}

function gotoHrefDownload(uri)
{
	openDetailPopup( BASEURI + "/view/popup/download.jsp?" + uri, 200, 10, true );
}

function gotoHref3D(uri)
{
	openDetailPopup( BASEURI + uri, 650, 400, true );
}

function gotoHrefDealerMap(uri)
{
	openDetailPopup( uri, 810, 645, true );
}

function gotoHrefConfiguratorSendFriend(uri)
{
	openDetailPopup( uri, 450, 300, true );
}

function gotoHrefProfileRememberPassword(uri)
{
	openDetailPopup( uri, 450, 280, true );
}

// resource handling -------------------------------------

function trackUri(uri)
{
	var path = uri + "";
	if ( path.indexOf(".xml") )
		path = path.split(".xml").join(".fsp");

	trackGA(path);
	trackSophus(path);
	
	$.fn.shareThis.currentUri = path;

}

/**
  * Google analytics
  */
function trackGA(path)
{
	try
	{
		for ( var i=0;i<HWPGaTrackers.length;i++ )
			HWPGaTrackers[i]._trackPageview(path);		
	}
	catch ( e )
	{
	}
}

/**
  * Sophus
  */
function trackSophus(path)
{
	try
	{
		tc_log(path);
	}
	catch ( e )
	{
	}
}

/**
 * Share this
 */
$.fn.shareThis = function()
{
	var m_this = this;
	
	function setup()
	{
		$(".sample", m_this).mouseover(function(){
			$(".data", m_this).show();
		});
		
		$(".data", m_this).bind("mouseleave", function(){
			$(this).fadeOut("fast");
		})
		
		$(".data a", m_this).click(function()
		{
			var currentUri = $.fn.shareThis.currentUri;
			
			if (currentUri.indexOf("http://")==-1)
				currentUri = "http://www.honda.es" + currentUri;
			var u = $(this).attr("href").split("${url}").join(escape(currentUri.split(".xml").join(".fsp")));
			u = u.split("${title}").join(escape($("title").text()));
			var w = window.open(u,'hondaSocial','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=auto');
			
			w.focus();
			return false;
		})
		
		m_this.show();
	}
	
	setup();
	
	return this;
}

$.fn.shareThis.currentUri = document.location.href;

$(document).ready(function()
{
	if (document.location.search.indexOf("fullscreen=true")!=-1)
	{
		$("head").append("<link rel=\"stylesheet\" href=\"" + BASEURI + "/res/css/sharethis.css\"/>");
		$.get(BASEURI + "/content/common/sharethis.html", function(data){
			$("#flash").append(data);
			$("#flash .shth").shareThis();
		})
	}
})
