var formTrackingPage = '';
var realConfCountryId = confCountryId;
if (realConfCountryId.indexOf("XQ") != -1) {
	realConfCountryId = 'com';
}
//formTrackingPage = '/' + realConfCountryId + '/' + confLanguageId + '/_common/tracking_redirect/form.html';

var getModelXML = buildValidServerRelativeUrl("/_common/shared/jsp/get_model_data.jsp");

function enableAjaxLoad() {
	var check = true;
	vipDeploymentType = "PROD";
	// check if browser is IE and current wcms-stage is edit or qa
	if (!jQuery.support.tbody) {
		if ((suspendAjaxLoad) || vipDeploymentType.toLowerCase() == "edit" || vipDeploymentType.toLowerCase() == "qa") {
			check = false;
		}
	}
	if (check) {
		return true;
	} else {
		return false;
	}
}
function rebuildTeaserReferences() {
	vipDeploymentType = "PROD";
	if (vipDeploymentType.toLowerCase() == "edit" || vipDeploymentType.toLowerCase() == "qa") {
		$(".relatedTeaser img").each(function() {
			$(this).attr({src: buildValidServerRelativeUrl($(this).attr("src"))});
		});
		$(".relatedTeaser a.rebuildLink").each(function() {
			$(this).attr({href: buildValidServerRelativeUrl($(this).attr("href"))});
		});
	}
	jQuery(function($) {
		$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	});
	$(".relatedTeaser img").each(function() {
		$(this).pngFix();
	});
}
function getModelToCompare(xmlFile) {
	currentIndex = 0;
	$.get(getModelXML + "?url=" + xmlFile, function(data) {
		for (var c1 = 0; c1 < rubricNames.length; c1++) {
			temp1 = "element[id='" + rubricNames[c1] + "']";
			$(data).find(temp1).each(function() {
				for (var c2 = 0; c2 < elementNames[c1].length; c2++) {
					temp2 = "text[id='" + elementNames[c1][c2] + "']";
					$(this).find(temp2).each(function() {
						$(document).find("table.datasheet tbody tr td.compare").eq(currentIndex).html($(this).text());
					});
					currentIndex++;
				}
			});
		}
		pos = xmlFile.lastIndexOf("/");
		$(data).find("imageelement[id='model_image_small']").each(function() {
			temp3 = xmlFile.substring(0, pos + 1) + $(this).find("url").attr("href");
			//@revisit: not clean
			temp3 = temp3.replace("localhost:9560", "www.bmw-motorrad.com");
			$(document).find("#cmprMdlImgBright").attr({src: /*VIPURL*/temp3});
			//set dark image too as fallback
			$(document).find("#cmprMdlImgDark").attr({src: /*VIPURL*/temp3});
		});
		$(data).find("imageelement[id='model_image_small_dark']").each(function() {
			temp3 = xmlFile.substring(0, pos + 1) + $(this).find("url").attr("href");
			//@revisit: not clean
			temp3 = temp3.replace("localhost:9560", "www.bmw-motorrad.com");
			$(document).find("#cmprMdlImgDark").attr({src: /*VIPURL*/temp3});
		});
	});
	if (currentIndex == 0) {
		$(document).find("table.datasheet tbody tr td.compare").each(function() {
			$(this).html("&nbsp;");
		});
		$(document).find("#cmprMdlImgBright").attr({src: transGif});
		$(document).find("#cmprMdlImgDark").attr({src: transGif});
	}
}
function changeModel() {
	$("#modelToCompare").change(function(e) {
		getModelToCompare($("#modelToCompare").val());
		//trackFormEvent('selectmodel', 'compared model 1', $(this).text(), formTrackingPage);
	});
}
jQuery.preloadImages = function() {
	for (var i = 0; i < arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}
var gallerySetHeight = function(size, returnValue) {
	if (!document.getElementById) return true;
	size = isNaN(size) ? 100 : Number(size);
	$("#gallery").animate({height: size}, {queue: false, duration: 300});
	if (!returnValue) return false;
};
function paramRequest(queryString) {
	if (queryString == '') return;
	var requestString = unescape(queryString);
	requestString = requestString.slice(1);
	var pairs = requestString.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var name = pairs[i].substring(0, pairs[i].indexOf("="));
		var value = pairs[i].substring(pairs[i].indexOf("=") + 1, pairs[i].length);
		this[name] = value;
	}
}

$(document).ready(function() {
	//$(document).pngFix();

	/* main */
	var loadAdditionalFunctions = function() {
		var flashSource = "";
		var flashParam  = "";
		var vSpinWidth = 652;
		var vSpinHeight = 489;
		var vColorID = "";
		var vBgColor = "";
		var vExpressInstall = "";
		var vScaleMethod = "";
		var vOpen = "";
		var vPath = "";
		$("#contentMain").find("#spin360 img").each(function() {
			vSpinWidth = $(this).attr("width");
			vSpinHeight = $(this).attr("height");
		});
		$("#contentMain").find("#spin360 .fileReferences a[title='flashSrc']").each(function() {
			flashSource = $(this).attr("href");
			flashSourceParamList = new paramRequest(flashSource);
			vBgColor = flashSourceParamList['bgColor'];
			flashSource = flashSource.slice(0);
		});
		$("#contentMain").find("#gallery .fileReferences a[title='flashSrc']").each(function() {
			flashSource = $(this).attr("href");
			flashSourceParamList = new paramRequest(flashSource);
			vExpressInstall = flashSourceParamList['expressInstall'];
			flashSource = flashSource.slice(0);
		});
		$("#contentMain").find("#spin360 .fileReferences a[title='flashPrm']").each(function() {
			flashParam  = $(this).attr("href");
			flashParamParamList = new paramRequest(window.location.search);
			vColorID = flashParamParamList['colorID'];
		});
		$("#contentMain").find("#gallery .fileReferences a[title='flashPrm']").each(function() {
			flashParam  = $(this).attr("href");
			flashParamParamList = new paramRequest(flashParam);
			vScaleMethod = flashParamParamList['scaleMethod'];
			vOpen = flashParamParamList['open'];
			vPath = flashParamParamList['path'];
			flashParam = flashParam.slice(0);
		});
		if (flashSource != "") {
			if (flashParam != "") {
				$("#contentMain").find("#spin360").each(function() {
					if (!vColorID) {
						$(this).flashembed({src: flashSource, version: [9, 0], bgcolor: vBgColor, width: vSpinWidth, height: vSpinHeight}, {configuration: flashParam});
					} else {
						$(this).flashembed({src: flashSource, version: [9, 0], bgcolor: vBgColor, width: vSpinWidth, height: vSpinHeight}, {configuration: flashParam, colorID: vColorID});
					}
				});
				$("#contentMain").find("#gallery").each(function() {
					if (!vPath) {
						$(this).flashembed({src: flashSource, version: [9, 0], expressInstall: vExpressInstall, width: '100%', height: '100%', wmode: 'opaque'}, {configuration: flashParam, open: vOpen, scaleMethod: vScaleMethod});
					} else {
						$(this).flashembed({src: flashSource, version: [9, 0], expressInstall: vExpressInstall, width: '100%', height: '100%', wmode: 'opaque'}, {configuration: flashParam, open: vOpen, path: vPath, scaleMethod: vScaleMethod});
					}
				});
			} else {
				$("#contentMain").find("#spin360").each(function() {
					$(this).flashembed({src: flashSource, version: [9, 0], bgcolor: vBgColor, width: vSpinWidth, height: vSpinHeight, wmode: 'opaque'});
				});
			}
		}
		$("#contentMain").find("#spin360").each(function() {
			$(this).animate({width: vSpinWidth, height: vSpinHeight}, {queue: false, duration: 300});
		});
		$("#contentMain").find("#gallery").each(function() {
			$(this).animate({width: "640px", height: "86px"}, {queue: false, duration: 300});
			gallerySetHeight();
		});
		changeModel();
		//preload images
		$("a[rel='color']").each(function(){
			var link = $(this).attr("href");
			jQuery("<img>").attr({src: link});
		});
	}
	loadAdditionalFunctions();

	/* navigation */
	if (enableAjaxLoad() == true) {
		$(".contentNavigation ul.nav1st li a").bind("click", function(e){
			$("#contentWrapper").html('<div class="loadingWrapper"><div class="isLoading"></div></div>');
			$(".contentNavigation ul.nav1st").find("li").each(function() {
				$(this).removeClass("active");
			});
			var link = $(this).attr("href");
			$("#contentWrapper").load(link + " #contentMain", loadAdditionalFunctions);
			$(this).parents("li").each(function() {
				$(this).addClass("active");
			});
			return false;
		});
	}

	/* switch model color */
	$(".bikeModel a[rel='color']").live("click", function(){
		var imageSrc = $(this).attr("href");
		$("#bigPicture").fadeTo("slow", 0.01, function(){
			$("#bigPicture img").attr({src: imageSrc});
		});
		$("#bigPicture").fadeTo("slow", 1.0);
		return false;
	});

	/* compare models and datasheet */
	$("#contentMain table.datasheet tbody tr").live("mouseover", function(){
		$(this).addClass("invertColor");
		$(this).find("td").each(
			function() {
				$(this).addClass("invertColor");
			}
		);
		$(this).find("th").each(
			function() {
				$(this).addClass("invertColor");
			}
		);
	});
	$("#contentMain table.datasheet tbody tr").live("mouseout", function(){
		$(this).removeClass("invertColor");
		$(this).find("td").each(
			function() {
				$(this).removeClass("invertColor");
			}
		);
		$(this).find("th").each(
			function() {
				$(this).removeClass("invertColor");
			}
		);
	});

	/* download model colour pictures */
	$(".bikeModel a[rel='download']").live("click", function(){
		var link = $(this).attr("href");
		downloadExternal(getFullPath(self.location.href, link));
		return false;
	});

	/* switch teaser items */
	$(".relatedTeaser a[rel='switchTeaser']").live("click", function(e){
		var teaserLink = $(this).attr("href");
		pos = teaserLink.search("#");
		teaserLink = teaserLink.slice(pos);
		$(this).closest(".relatedTeaser").fadeOut("fast", function(){
			$(teaserLink).fadeIn("normal");
		});
		return false;
	});

	/* select boxes (usually ecom links at .com) */
	$("select[name='selectConfiguration']").change(function(e) {
		window.open($(this).val());
	});

	/* jquery.plugin.slimbox2 save function for download-teaser */
	$("#lbCaption a.lbCaption").live("click", function(){
		link = $("div#lbImage").css("background-image").replace(/^url|[\(\"\"\)]/g, '');
		link = "javascript:downloadExternal(buildValidServerRelativeUrl('" + link + "'))";
		$(this).attr({href: link});
	});

	/* image gallery - animate height */
	$("#gallery").live("click", function(e){
		gallerySetHeight();
	});
	
});

