if (typeof vcc != "object")	vcc = {};

$(window).ready(function() {
	vcc.fixIePng("#content");
});

function getScrollXY() { 
	var scrOfX = 0, scrOfY = 0; 
	if( typeof( window.pageYOffset ) == 'number' ) { 
		//Netscape compliant 
		scrOfY = window.pageYOffset; 
		scrOfX = window.pageXOffset; 
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { 
		//DOM compliant 
		scrOfY = document.body.scrollTop; 
		scrOfX = document.body.scrollLeft; 
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { 
		//IE6 standards compliant mode 
		scrOfY = document.documentElement.scrollTop; 
		scrOfX = document.documentElement.scrollLeft; 
	} 
	return {X:scrOfX, Y:scrOfY}; 
} 

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{X:myWidth, Y:myHeight} 
}

/*
 * Returns the value of a query string parameter, or null if it does not exist
 */
vcc.getParameter = function(parameterName) {
    var queryString = window.location.search;
    if (queryString.length > 0 && parameterName.length > 0) {
        if (parameterName.substring(parameterName.length - 1) != "=")
            parameterName += "=";
        var start = queryString.indexOf(parameterName);
        if (start != -1) {
            start += parameterName.length;
            var end = queryString.indexOf("&", start);
            if (end == -1)
                end = queryString.length;
            return decodeURIComponent(queryString.substring(start, end));
        }
    }
    return null;
}

vcc.center = function(elm, bHorizontally, bVertically) {
	if (elm == null)
		return;
	var windowDim = getWindowSize(); 
	var popupHeight = $(elm).height(); 
	var popupWidth = $(elm).outerWidth(); 
	//alert(popupWidth);
	var scroll = getScrollXY(); 
	//centering 
	if (bHorizontally) {
		$(elm).css({ 
			"position": "absolute",
			"left": windowDim.X/2-popupWidth/2 + scroll.X 
		});
	}
	if (bVertically) {
		$(elm).css({ 
			"position": "absolute", 
			"top": windowDim.Y/2-popupHeight/2 + scroll.Y-15
		});
	}
	
}

vcc.flashLightBoxWidth = null;
vcc.flashLightBoxHeight = null;
vcc.flashVarsLightBox = null;
vcc.centerLightBoxVertically = false;

vcc.clone = function(obj) {
	if(obj == null || typeof(obj) != 'object')
        return obj;
	var newObj = new obj.constructor()
	for (var key in obj) {
		newObj[key] = vcc.clone(obj[key]);
	}
	return newObj;
}

vcc.flashLightbox = function(sUrl, iWidth, iHeight, bDisableBorders) {
	vcc.flashVarsLightBox = vcc.clone(vcc.flashVars);
	vcc.flashVarsLightBox.flashobjectid = "flashLightBoxContainer";
	if (sUrl.indexOf(".swf") > -1) {
		//Regular static flash, load it with swf object
		vcc.showOverlay(vcc.hideOverlay, "<div id=\"flashLightBoxContainer\" style=\"width: " + iWidth + "px;height: \"" + iHeight + "px;\"></div>", iWidth, iHeight, bDisableBorders);
		swfobject.embedSWF(sUrl, "flashLightBoxContainer", iWidth, iHeight, "9.0.0", "/_layouts/VolvoCars.SharePoint.Features.Site3/swf/expressInstall.swf", vcc.flashVarsLightBox, { wmode: "window", allowscriptaccess: "always", allowfullscreen: "true", bgcolor: "#000000"  }, { id: "flashLightBoxContainer", "class": "flash" } );
	} else {
		//Flashcontent page. Get the swf with content url and settings from the page using AJAX
		//If width and height has been passed to the function, use it instead of the dimensions in the aspx page
		vcc.flashLightBoxWidth = iWidth;
		vcc.flashLightBoxHeight = iHeight;
		if (sUrl.indexOf("?") > -1) {			
			var params = sUrl.substring(sUrl.indexOf("?"));
			params.replace(
				new RegExp("([^?=&]+)(=([^&]*))?", "g"),
				function($0, $1, $2, $3) {
					vcc.flashVarsLightBox[$1] = $3;
				}
			);			
			sUrl = sUrl.substring(0, sUrl.indexOf("?"));			
		}		
		$.getJSON(sUrl + "?json=true", function(data){
			if (vcc.flashLightBoxWidth != null && vcc.flashLightBoxWidth != 0 && vcc.flashLightBoxWidth != "") {
				vcc.showOverlay(vcc.hideOverlay, "<div id=\"flashLightBoxContainer\" style=\"width: " + vcc.flashLightBoxWidth + "px;height: \"" + vcc.flashLightBoxHeight + "px;\"></div>", vcc.flashLightBoxWidth, vcc.flashLightBoxHeight, bDisableBorders);
				swfobject.embedSWF(data.url, "flashLightBoxContainer", vcc.flashLightBoxWidth, vcc.flashLightBoxHeight, data.version, "/_layouts/VolvoCars.SharePoint.Features.Site3/swf/expressInstall.swf", vcc.flashVarsLightBox, { wmode: data.wmode, allowscriptaccess: "always", allowfullscreen: "true", bgcolor: data.bgcolor  }, { id: "flashLightBoxContainer", "class": "flash" } );
			} else {
				vcc.showOverlay(vcc.hideOverlay, "<div id=\"flashLightBoxContainer\" style=\"width: " + data.width + "px;height: \"" + data.height + "px;\"></div>", data.width, data.height, bDisableBorders);
				swfobject.embedSWF(data.url, "flashLightBoxContainer", data.width, data.height, data.version, "/_layouts/VolvoCars.SharePoint.Features.Site3/swf/expressInstall.swf", vcc.flashVarsLightBox, { wmode: data.wmode, allowscriptaccess: "always", allowfullscreen: "true", bgcolor: data.bgcolor  }, { id: "flashLightBoxContainer", "class": "flash" } );
			}
		});
	}
	
}

vcc.showOverlay = function(oOnClickFunction, sOverlayHtml, iOverlayWidth, iOverlayHeight, bDisableBorders) {
	var oOverlay = document.getElementById("overlay");
	if (oOverlay == null) {
		oOverlay = document.createElement("div")
		oOverlay.style.display.opacity = 0;
		oOverlay.id = "overlay";
		document.body.appendChild(oOverlay);
		oOverlay.onclick = oOnClickFunction;
		//$("#overlay").hide();	
	}
		
	vcc.resizeOverlay();
	
	$("#overlay").fadeTo(0, 0.7);
	$("#overlay").show();
	
	if (sOverlayHtml == null)
		return;
		
	var oOverlayContainer = document.getElementById("overlay-container");
	if (oOverlayContainer == null) {
		oOverlayContainer = document.createElement("div");
		oOverlayContainer.id = "overlay-container";
		document.body.appendChild(oOverlayContainer);
	}
	
	oOverlayContainer.style.width = (iOverlayWidth) ? iOverlayWidth + "px" : "auto";
	oOverlayContainer.style.height = (iOverlayHeight) ? iOverlayHeight + "px" : "auto";
	
	oOverlayContainer.innerHTML = sOverlayHtml + "<a href=\"#\" id=\"overlay-closebutton\" onclick=\"vcc.hideOverlay();return false;\"></a>";
	if (bDisableBorders) {
		$("#overlay-container").css("padding", 0);
	} else {
		$("#overlay-container").corner("3px cc:#4c4c4c sc:#fff");
		$("#overlay-container").css("padding", "8px");
	}
	$("#overlay-container").show();
	vcc.center(oOverlayContainer, true, vcc.centerLightBoxVertically);
	
	$(window).bind("resize", vcc.handleOverlayWhenResized);
}

vcc.handleOverlayWhenResized = function() {
	vcc.resizeOverlay();
	vcc.center(document.getElementById("overlay-container"), true, vcc.centerLightBoxVertically);
}

//Updates the light box dimensions

vcc.updateOverlaySize = function(iOverlayWidth, iOverlayHeight) {
	var oFlashLightBoxContainer = document.getElementById("flashLightBoxContainer");
	if (oFlashLightBoxContainer) {
		oFlashLightBoxContainer.style.width = (iOverlayWidth) + "px";
		oFlashLightBoxContainer.style.height = (iOverlayHeight) + "px";
	}
	
	var oOverlayContainer = document.getElementById("overlay-container");
	if (oOverlayContainer) {
		oOverlayContainer.style.width = (iOverlayWidth) + "px";
		oOverlayContainer.style.height = (iOverlayHeight) + "px";
		vcc.center(oOverlayContainer, true, vcc.centerLightBoxVertically);
	}	
}

vcc.resizeOverlay = function() {
	var oOverlay = document.getElementById("overlay");
	if (oOverlay == null)
		return;
	var arrayPageSize = vcc.getPageSize();
	oOverlay.style.width = arrayPageSize[0] + "px";
	oOverlay.style.height = arrayPageSize[1] + "px";
}

vcc.hideOverlay = function() {
	$(window).unbind("resize", vcc.handleOverlayWhenResized);
	$("#overlay").fadeOut(500);
	if ($("#overlay-container #flashLightBoxContainer").length > 0) {
		swfobject.removeSWF("flashLightBoxContainer");
	} else if ($("#overlay-container #large").length > 0) {
		swfobject.removeSWF("large");
	}
	$("#overlay-container").replaceWith("<div id=\"overlay-container\"></div>");
	$("#overlay-container").hide();
	vcc.centerLightBoxVertically = false;
}

vcc.getPageSize = function() {	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

vcc.centerFlash = function(sFlashId, iWidth, iHeight) {	
	var oFlashObject = $("#" + sFlashId);	
	var oWindow = $(window);
	
	var x = (oWindow.width() / 2) - (iWidth / 2);
    var y = (oWindow.height() / 2) - (iHeight / 2);
    
    x = parseInt(x + oWindow.scrollLeft());
    y = parseInt(y + oWindow.scrollTop());
	
    var sContainerId = sFlashId + "_centerContainer";
    
    if (document.getElementById(sContainerId) == null) {
		$("form").append("<div id=\"" + sContainerId + "\"></div>");
		oFlashObject.remove().appendTo("#" + sContainerId);
		oWindow.bind('resize', function() {
			vcc.centerFlash(sFlashId, iWidth, iHeight);
		});	
    }    
	
	$("#" + sContainerId).css( {
		"position": "absolute",
		"top": y,
		"left": x,
		"z-index": 30000
		});		
}

vcc.fullscreenFlash = function(sFlashId) {
	var oFlashObject = $("#" + sFlashId);	
	var sContainerId = sFlashId + "_fullscreenContainer";
	var oContainer = document.getElementById(sContainerId);
	var oWindow = $(window);
	
	if (oContainer == null) {
		$("form").append("<div id=\"" + sContainerId + "\"></div>");
		oContainer = document.getElementById(sContainerId);
		$(oContainer).css( {
			"position": "absolute",
			"top": 0,
			"left": 0
		});
		oFlashObject.remove().appendTo("#" + sContainerId);
		oWindow.bind('resize', function() {
			vcc.fullscreenFlash(sFlashId);
		});	
    }    
    
    $(oContainer).css( {
		"width": oWindow.width(),
		"height": oWindow.height()
    });
}

vcc.openWindow = function(sURL, sName, sOptions, bDoNotReturnWindow) {
	if (!sOptions) sOptions = "location=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,";
	else {
		//parse the sOptions variable to get width and height
		var aWidth = sOptions.match(/width=[0-9]+/i);
		var aHeight = sOptions.match(/height=[0-9]+/i);
		if (aWidth != null) {
			//Center window horizontally
			var iWidth = aWidth[0].substring(aWidth[0].lastIndexOf("=") + 1);
			var x = (screen.width - iWidth) / 2;
			x = (x<0) ? 0 : x
			sOptions += ",left=" + x;
		}
		if (aHeight != null) {
			//Center window vertically
			var iHeight = aHeight[0].substring(aHeight[0].lastIndexOf("=") + 1);
			var y = (screen.height - iHeight) / 2;
			y = (y<0) ? 0 : y
			sOptions += ",top=" + y;
		}
	}
	var popup = window.open(sURL,sName,sOptions);
	if (!bDoNotReturnWindow)
		return popup;
}

vcc.trackEvent = function(sUrl, sTitle, sGroup, sSubGroup) {
	if (typeof(pageTracker) == "object") {
		//Google analytics
		var sGoogleUrl = sUrl;
		if (sGoogleUrl.indexOf("/") != 0)
			sGoogleUrl = "/" + sGoogleUrl;		
		sGoogleUrl = document.location.pathname + sGoogleUrl;		
		pageTracker._trackPageview(sGoogleUrl);
	}	
}

vcc.trackCampaignEvent = function(category, action, optional_label, optional_value) {
	if (typeof(pageTracker) == "object") {
		pageTracker._trackEvent(category, action, optional_label, optional_value);
	}
}

//Loops through all links on the page, and attaches an onclick statistics call for external ones
vcc.initExternalTracking = function() {
	if (typeof(pageTracker) != "object")
		return;
	pageTracker._setAllowLinker(true);
	var sDomain = document.location.hostname.toLowerCase();
	$.each($("#content a"), function() {
		var sLinkUrl = this.href.toLowerCase()
		if (sLinkUrl.indexOf("http") > -1 && sLinkUrl.indexOf(sDomain) == -1) {
			$(this).click(function() {
				pageTracker._link(this.href);
				return false;
			});
		}
	});
}

vcc.flashObj = function(sUrl, iWidth, iHeight, sRequiredVersion, oParams, oAttributes) {
	this.url = sUrl;
	this.width = iWidth;
	this.height = iHeight;
	this.requiredVersion = sRequiredVersion;
	this.params = oParams;
	this.attributes = oAttributes;
}

//Searches the vcc.aModels array and returns the first model that matches the key.
vcc.getModel = function(key) {
	key = key.toLowerCase();
	for (var i = 0; i < vcc.aModels.length; i++) {
		var oModel = vcc.aModels[i];
		if (oModel.ModelWebTitle.toLowerCase() == key || oModel.ModelWebName.toLowerCase() == key
			|| oModel.Name.toLowerCase() == key || oModel.ShortName.toLowerCase() == key
			|| oModel.ModelCode.toLowerCase() == key) {
			return oModel;
		}
	}
	return null;
}

vcc.fixIePng = function(sElementSelector) {
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {
		//Images
		if (sElementSelector == null)
			sElementSelector = "#content";
		$("img[src$=.png]", $(sElementSelector)).each(function() {
			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}
			//alert(strNewHTML);
			jQuery(this).hide();
			jQuery(this).after(strNewHTML);
		});		
		// Background-images
		/*if (vcc.pngBgsToFix) {
			var arAll = document.getElementsByTagName("*");
			for (var i=0; i<arAll.length; i++) {
				if (arAll[i] && arAll[i].currentStyle && arAll[i].currentStyle.backgroundImage && arAll[i].currentStyle.backgroundImage.indexOf("url(") > -1) {
					var sUrl = arAll[i].currentStyle.backgroundImage.substring(5, arAll[i].currentStyle.backgroundImage.length - 2);
					if (sUrl.substring(sUrl.length-3, sUrl.length) == "png") {
						for (var j=0; j<vcc.pngBgsToFix.length; j++) {
							if (sUrl.indexOf(vcc.pngBgsToFix[j]) > -1) {
								arAll[i].style.backgroundImage = "none";
								arAll[i].style.filter += "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + sUrl + "', sizingMethod='scale')";
							}
						}
					}
				}
			}
		}*/
	}
}

function openAccesorios(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=1000,height=600');");
}
function openDemos(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=473,height=550');");
}