/* RightCol */
/* search */
/*
$(function() {
	$("#qq-wert").addClass(searchInputClass);
	
	$("#qq-wert").focus(function () {
		$("#qq-wert").addClass("input-active");
		 
		var currentVal = $(this).val();
		
		// Test
		// console.log (currentVal);		
		
		// delete initial value
		if (currentVal == "&#1087;&#1086;&#1080;&#1089;&#1082; &#1089;&#1090;&#1072;&#1090;&#1100;&#1080;") {
			$(this).val("");		
		}
	});
	
	$("#rightcol-searchbox-button").click(function (e) {
		var currentVal = $("#qq-wert").val();
		
		// send 
		if (currentVal != "&#1087;&#1086;&#1080;&#1089;&#1082; &#1089;&#1090;&#1072;&#1090;&#1100;&#1080;") {
			// Tracking
			countclick(this, "Mag-Response-Search");
			$("#magazine-rightcol-search-form").submit();		
		} else {
			$("#qq-wert").focus();
			return false; 
		}	
	});
});
*/
/* alreadyRead */
$(function() {
	$("#cms-magazine-rightcol-alreadyRead-id2").click(function () {
		var dt = new Date();
		var ts = dt.getTime(); 		
	
		// Test 
		// console.log("as24_cms_mag.js AlreadyRead" + alreadyRead_refUrl + "-" + alreadyRead_artId);
		
		$.ajax({
			url: 'as24-magazine-rightcol-alreadyread-handler.aspx',
		 	data: {
				mode:'alreadyreadrefresh',
				article:alreadyRead_artId,
				ref:alreadyRead_refUrl, 
				ts:ts
			},			  
		  	success: function(data) {
				// Tracking 
				countclick(this, "Mag-Response-AlreadyReadReload");				
				
				$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent2").fadeOut("normal",function() {	
					$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent2").html(data);

					$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent2").fadeIn("normal",function () {
						// document.getElementById("refreshbutton").src='';
					});

				});			
		  	}
		});			
	
	});	
	$("#cms-magazine-rightcol-alreadyRead-id1").click(function () {
		var dt = new Date();
		var ts = dt.getTime(); 		
		
		// Test 
		// console.log("as24_cms_mag.js AlreadyRead" + alreadyRead_refUrl + "-" + alreadyRead_artId);
		
		$.ajax({
			url: 'as24-magazine-rightcol-alreadyread-handler.aspx',
		 	data: {
				mode:'alreadyreadrefresh',
				article:alreadyRead_artId,
				ref:alreadyRead_refUrl,
				ts:ts
			},			  
		  	success: function(data) {
				// Tracking 
				countclick(this, "Mag-Response-AlreadyReadReload");				
				
				$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent1").fadeOut("normal",function() {	
					$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent1").html(data);

					$(".cms-magazine-rightcol-alreadyRead .alreadyreadcontent1").fadeIn("normal",function () {
						// document.getElementById("refreshbutton").src='';
					});

				});			
		  	}
		});			
	
	});	
});
/* messages */
function messageShowContent (activeTab) {
	// Test 
	// console.log (activeTab); 
	
	// highlight activeTab 
	$(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li").removeClass("active");
	$(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li." + activeTab).addClass("active");
	
	// hide content 
	$(".cms-magazine-rightcol-messages .content").hide();
	
	// show content and tabClicks
	$(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li").unbind(); 	
	if (activeTab == "1") {
	  	$(".cms-magazine-rightcol-messages .content.1").show();		  
		$(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li:not(.active)").click (function () {
			messageShowContent("2");
		}); 		  
	} else {
		$(".cms-magazine-rightcol-messages .content.2").show();		
		$(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li:not(.active)").click (function () {
			messageShowContent("1");
		}); 		
	}
}
$(function() {
	// Test 
	// console.log($(".cms-magazine-rightcol-messages .cms-tabbedbox-tabs li:not(.active)"));
	
	messageShowContent("1");

});
/* as24 Search */
$(function() {
	$("#cms-usedcarsearch-magazine-btn").click(function() {
		// Tracking
		countclick(this, "Mag-Response-UsedCarSearch");
	
		runSearch(); 		
	}); 
});
/* tracking  */
$(function() {

});
/* adTag check */
function adtagHide(item) {
	// for inpage ads
	if($(item).find(".adtag").height()<100) {	
		$(item).parent().hide();
	}
	// for iframe ads
	if($(item + ' #galleryAdRectangle').contents().find("html").height()<100 && adtagsIframesBool) {	
		$(item).parent().hide();
	}		
	
}
function adtagShow(item) {
	$(item).parent().show();
}
$(function() {
	adtagHide("#as24-magazine-rightcol-adtag-1"); 
	adtagHide("#as24-magazine-rightcol-adtag-2"); 
});
/* evote */
function animateRightColVoting() {
   	$(".evotebar").each(function(){  
		// Test 
		// console.log(this);   	
   
       var percentage = $(this).next().text();
	   var precentageCleared = percentage.replace(/%/g,"");
	   
	   // Test 
	   // console.log (precentageCleared);	   
	    
	   precentageCleared = precentageCleared * 2.6; 
	   
       $(this).css({width: "0px"}).animate({  
                 width: precentageCleared }, 'fast');  
   });  
}
function enableRightColVoting(evoteId) {
	// enable voteBtn
	$("#cms-magazine-rightcol-vote-button-id").removeClass("lightBlueButtonDeactive"); 	
	$("#cms-magazine-rightcol-vote-button-id").click(function() {
		doRightColVoting(evoteId);				
	}); 
}
function loadRightColVoting(evoteId) {
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	

	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			// show loadIcon 
			// ToDo
		},
		url: "displayevote.aspx?rightcol=true&ajax=" + tsEvote + "&nohide=1&evote=" + evoteId,
		success: function(data){
			// Test 
			// console.log ("as24_cms_mag.js" + data);
			
			// remove loading 
			$(".cms-magazine-rightcol-vote-loading").hide();
			$("#cms-magazine-rightcol-vote-id").html(data);
			
			// animate
			animateRightColVoting();			
	 	}
	});
}
function doRightColVoting(evoteId) {
	var evoteform = $("#cms-magazine-rightcol-vote-form");
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	
	
	// Test 
	// console.log(evoteform);
	
	$.ajax({
	 	type: "GET",
		beforeSend:function(){
			// show loadIcon 
			// ToDo		
		},				
		url: "displayevote.aspx?rightcol=true&ajax=" + tsEvote + "&nohide=1&evote=" + evoteId +"&" + evoteform.serialize(),
		success: function(data){
			// Test 
			// console.log(data); 
			
			$("#cms-magazine-rightcol-vote-id").html(data);
			animateRightColVoting();
			
			// Tracking
			countclick(this, "Mag-Response-eVote");
			
	 	}
	});
}
$(function() {
	// load evote
	if ( typeof(rigthColEvoteId) != "undefined") {
		// Test	
		// console.log (rigthColEvoteId); 
		
		if (rigthColEvoteId != 99) {
			loadRightColVoting(rigthColEvoteId);	
		} else {
			$(".cms-magazine-rightcol-vote").hide();
		}
	} else {
		$(".cms-magazine-rightcol-vote").hide();
	}
});

/* Article */
/* paging */
function doPage (page){
	// Test 
	// console.log(page);

	$("#pageLinks input[name='page']").val(page);
	//document.pageLinks.page.value = page;
	document.getElementById("pageLinks").submit();
}
/* evote */
function animateArticleVoting() {
   	$(".evotebar").each(function(){  
		// Test 
		// console.log("test");   	
   
       var percentage = $(this).next().text();
	   var precentageCleared = percentage.replace(/%/g,"");
	   
	   // Test 
	   // console.log (precentageCleared);	   
	    
	   precentageCleared = precentageCleared * 1.5; 
	   
	   // Test 
	   // console.log (precentageCleared);	  	   
	   
       $(this).css({width: "0px"}).animate({  
                 width: precentageCleared }, 'fast');  
   });  
}
function enableArticleVoting(evoteId) {
	// Test 
	// console.log ("Testausgabe " + evoteId); 	

	// enable voteBtn	
	$("#cms-magazine-article-eVote-button-id").removeClass("lightBlueButtonDeactive"); 	
	$("#cms-magazine-article-eVote-button-id").click(function() {
		doArticleeVoteVoting(evoteId);				
	}); 
}
function doArticleeVoteVoting(evoteId) {
	var evoteform = $("#cms-magazine-article-vote-form");
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	
	
	// Test 
	// console.log(evoteform);
	// console.log (evoteId);
	
	$.ajax({
	 	type: "GET",
		beforeSend:function(){
			// show loadIcon 
			// ToDo		
		},				
		url: "displayevote.aspx?inarticle=true&ajax=" + tsEvote + "&nohide=1&evote=" + evoteId +"&" + evoteform.serialize(),
		success: function(data){
			// Test 
			// console.log(data); 
			
			$("#cms-magazine-article-vote-id").html(data);
			animateArticleVoting();
			
			// Tracking
			// countclick(this, "Mag-Article-eVote");
	 	}
	});
}
function loadArticleVoting(evoteId) {
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	

	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			// show loadIcon 
			// ToDo
		},
		url: "displayevote.aspx?inarticle=true&ajax=" + tsEvote + "&nohide=1&evote=" + evoteId,
		success: function(data){
			// Test 
			// console.log ("as24_cms_mag.js" + data);
			
			// remove loading 
			$(".cms-magazine-article-vote-loading").hide();
			$("#cms-magazine-article-vote-id").html(data);
			
			// animate
			animateArticleVoting();			
	 	}
	});
}

function loadArticleResultEvote(evoteId,obj) {
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	

	var evotebody = $(obj).parent().parent().parent().parent().parent().parent();
	 
	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			evotebody.height(evotebody.height());
			evotebody.empty();
			evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
			evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");
			evotebody.parent().find("img").remove();			
		},		
		url: "displayevote.aspx?inarticle=true&ajax=" + tsEvote + "&nohide=1&evote=" + evoteId +"&displayresults=1",
		success: function(data){
			evotebody.replaceWith(data);
			
			// Test
			// console.log (this); 
			
			animateArticleVoting();
	 	}
	});
}

$(function() {
	// load evote
	if ( typeof(articleEvoteId) != "undefined" ) {	
		// Test
		// console.log (articleEvoteId); 
		
		if (articleEvoteId != 99) {
			loadArticleVoting(articleEvoteId);	
		}
	}
});
/* tellafriend */
$(function() {
	$(".cms-magazine-article-tellafriend-share").click(function(e) {	
		e.preventDefault();
		
		// get request link 
		var reqLink = $("#TAFmodalLink").attr("href");
		
		// Test 
		// console.log ("Test: " + reqLink); 
		
		$("#cms-magazine-article-tellafriend-container").load(reqLink, function() {
			$("#TAFform").validate({
				success: function(label) {
					// Test 
					// console.log (label.parent()); 
					
					label.parent().css({ height: 0 });
					label.parents(".item").find(".item-label-txt").removeClass("not-valid"); 
					label.parent().parent().removeClass("not-valid");
					label.remove();
				},
				errorPlacement: function(error, element) {
					// Test 
					// console.log(element.parent());
					
					element.parent().find(".validation-errors").css({ height: "auto" });
					
					element.parents(".item").find(".item-label-txt").addClass("not-valid"); 
					element.parent().addClass("not-valid");
					error.appendTo(element.parent().find(".validation-errors"));
				},			
				submitHandler: function() {
					onkeyup:false,
			   		$.ajax({
						type: "POST",
						url: "mz_tellafriend_test.aspx",
						data: $("#TAFform").serialize(),
						cache: false,
						beforeSend: function() {
							/*
							$("#messagesending").css("display","block");
							$("#TAFbtn").removeClass("button-emphasised").addClass("button-inactive");
							$("#TAFbtn").attr("disabled","disabled");
							*/
						},
						success: function(repost){
							$("#tellafriendSuccess").html(repost);
							$("#tellafriendSuccess").show();
							$("#tellafriend").remove();
							
							$("#cms-magazine-article-tellafriend-container").css({"min-height": 56});							
							
							setTimeout("$('#cms-magazine-article-tellafriend-container').dialog('close');",3000);
							
				   		}
				 	});
			   	},
				errorElement: "span",	
				errorClass: "not-valid"
			});
			
			$("#cms-magazine-article-tellafriend-container").dialog({
				modal:true, 
				resizable: false, 				
				width: 468
			});
			
			$(".contact-caption-close").click(function () {
				$("#cms-magazine-article-tellafriend-container").dialog("close"); 
			});			
		});
		

	}); 
});
/* gallery */
var galleryObj; 
var current; 
var galleryLoadedBool = false;
var disableRightBool = false; 
var disableLeftBool = true; 
var adtagsIframesBool = false;
var firstGalleryPicBool = true;
var wideGallery = false;
// Test
/*
console.log (location.hash);
console.log (location.href);
console.log (location.host);
console.log (location.search);
console.log (location.pathname);
*/
if(location.hash && location.hash != "" && location.hash.indexOf("gal=") > -1) {
	// ToDo
	// location.href="?" + location.hash.replace("#","");
}

/* start - old gallery functions */
function doChangeImage(ImageInGallery,Gallery,IndexInCarousel, onStartBool) {
	// adTag Infos
	var adTagOrd = Math.round(Math.random()*10000000000000000);
	var adTargeting = "";
	for (var i=0; i< galleryObj[Gallery].articleTopicKey.length; i++)
		adTargeting += escape("story=" + galleryObj[Gallery].articleTopicKey[i] + ";");
	
	// intial
	var img = $("img#galleryImage");
	$("#relatedGalleries").hide();
	img.hide();
	if(IndexInCarousel)
		current.indexInCarousel = IndexInCarousel;
	$(".galleryPreviewActive").removeClass("galleryPreviewActive");
	$(".galleryThumbActive").removeClass("galleryThumbActive");
	current.imageInGallery = ImageInGallery;
	
	// gab es einen Galleriewechsel durch press-thumbnail
	if (current.gallery ==  Gallery) {
		current.gallery = Gallery;
	} else {
		current.gallery = Gallery;
	}
	
	// Test 
	// console.log ("current.gallery: " + current.gallery);

	// calculate next and prev links 
	current.prevGallery = current.nextGallery = -1;
	current.prefHref = current.nextHref = "";		
	
	if(current.gallery == 0 && current.gallery < (current.numberOfGalleries-1) ) {
		current.nextGallery = current.gallery + 1;		
	} else if(current.gallery > 0 && current.gallery < (current.numberOfGalleries-1) ) {
		current.prevGallery = current.gallery - 1;
		current.nextGallery = current.gallery + 1;
	} else if(current.gallery == (current.numberOfGalleries-1) ) {
		current.prevGallery = current.gallery - 1;
	}
	
	var queryString = "?article=" + articleId + "&gal="; 
		
	if(current.imageInGallery == -1) {
		current.nextHref = queryString + galleryObj[current.gallery].id + "&image=0";
		if(current.prevGallery > -1)
			current.prevHref = queryString + galleryObj[current.prevGallery].id + "&image=" + (galleryObj[current.prevGallery].pictures.pictures.length-1);			
	} else {	
		if(!(current.imageInGallery == 0 && current.prevGallery == -1)) {
			if(current.imageInGallery == 0) {
				if(galleryObj[current.gallery].pictures.pictures.length > 1) {
					current.nextHref = queryString + galleryObj[current.gallery].id + "&image=" + (current.imageInGallery +1);
					if(current.prevGallery > -1)
						current.prevHref = queryString + galleryObj[current.prevGallery].id + "&image=" + (galleryObj[current.prevGallery].pictures.pictures.length-1);
				} else {
					if(current.prevGallery > -1)
						current.prevHref = queryString + galleryObj[current.prevGallery].id + "&image=" + (galleryObj[current.prevGallery].pictures.pictures.length-1);
					if(current.nextGallery > -1)
						current.nextHref = queryString + galleryObj[current.nextGallery].id + "&image=0";
				}	
			} else if(current.imageInGallery == galleryObj[current.gallery].pictures.pictures.length-1) {
				if(galleryObj[current.gallery].pictures.pictures.length > 1) {
					current.prevHref = queryString + galleryObj[current.gallery].id + "&image=" + (current.imageInGallery -1);
					if(current.nextGallery > -1)
						current.nextHref = queryString + galleryObj[current.nextGallery].id + "&image=0";
				} else {
					if(current.prevGallery > -1)
						current.prevHref = queryString + galleryObj[current.prevGallery].id + "&image=" + (galleryObj[current.prevGallery].pictures.pictures.length-1);
					if(current.nextGallery > -1)
						current.nextHref = queryString + galleryObj[current.nextGallery].id + "&image=0";
				}
			} else {
				current.nextHref = queryString + galleryObj[current.gallery].id + "&image=" + (current.imageInGallery +1);
				current.prevHref = queryString + galleryObj[current.gallery].id + "&image=" + (current.imageInGallery -1);
			}
		}
	}				
	
	// 20110825 only show one gallery and disable btn  
	if (current.gallery > 0) {
		// hide button 
		disableRightBool = true; 
		$(".gallery-tools-navi.galleryDoRight").addClass("inactive");
		
		$(".galleryDoLeftIcon").hide(); 
		$(".galleryDoRightIcon").hide(); 		
		
		// delete prevHref
		current.prevHref = ""	
	} else {
		// show button 
		disableRightBool = false; 		
		$(".gallery-tools-navi.galleryDoRight").removeClass("inactive");
		$(".gallery-tools-navi.galleryDoLeft").removeClass("inactive");
		
		$(".galleryDoLeftIcon").show(); 
		$(".galleryDoRightIcon").show(); 		
		
		// first image ?
		if (current.imageInGallery  == 0) {
			$(".galleryDoLeftIcon").hide();
			$(".gallery-tools-navi.galleryDoLeft").addClass("inactive");		
		}
	}
	
	// Test 
	// testGallery("current.toSource(): " + current.toSource()); 
	
	// change the image and other infos 
	var sProp7 = "mz_gallery_";	
	s.pageName="Mag-Gallery";
	s.pageName= omnitureSitename + "-Gallery";
	var adjustedHeight, adjustedWidth, originalHeight, originalWidth, imageRatio;
	if(current.imageInGallery != -1) {
		// not "-1" means this is a regular image 
		var nextImage = galleryObj[current.gallery].pictures.pictures[current.imageInGallery];
		img.attr("title",nextImage.text);
		img.attr("alt",nextImage.text);
		img.attr("src",galleryObj[current.gallery].baseUrlGallery + nextImage.url);
		
		// alte und neue Masse, Seitenverhaeltnis
		originalHeight = nextImage.sizeY;
		originalWidth = nextImage.sizeX;
		imageRatio = originalWidth/originalHeight;
		adjustedHeight = originalHeight;
		adjustedWidth = originalWidth;
		
		// erstes Bild gross bedeutet Gallery Grossansicht
		if (firstGalleryPicBool && originalWidth > 639) {
			wideGallery = true;
		}
		
		if(wideGallery == false) {
			// Hochformat
			if((originalHeight >= originalWidth) && originalHeight > 317) {
				adjustedHeight = 317;
				adjustedWidth = parseInt(317 * imageRatio);
			} else if ((originalWidth >= originalHeight) && originalWidth > 476 && imageRatio < 1.5) {
				// Querformat
					adjustedHeight = 317;
					adjustedWidth = parseInt(317 * imageRatio);
			} else if ((originalWidth >= originalHeight) && originalWidth > 476 && imageRatio >= 1.5) {
				// Querformat
				adjustedWidth = 476;
				adjustedHeight = parseInt(476 / imageRatio);
			} else if(originalHeight > 317 && originalWidth > 476) {
				// Andere Ausreiser
				if(imageRatio >= 1.5) {
					adjustedWidth = 476;
					adjustedHeight = parseInt(476 / imageRatio);
				} else {
					adjustedHeight = 317;
					adjustedWidth = parseInt(317 * imageRatio);
				}
			}
			$(".cms-magazine-article-gallery-toolColumn").show();
			$(".cms-magazine-toolColumn-topTools").html("&nbsp;");  // nimm tools auf Ueberschriftenhoehe raus
			$("#cms-magazine-article-gallery-toolColumn").show();
		}
		else {
			// Hochformat
			if((originalHeight >= originalWidth) && originalHeight > 426) {
				adjustedHeight = 426;
				adjustedWidth = parseInt(426 * imageRatio);
			} else if ((originalWidth >= originalHeight) && originalWidth > 640 && imageRatio < 1.5) {
				// Querformat
					adjustedHeight = 426;
					adjustedWidth = parseInt(426 * imageRatio);
			} else if ((originalWidth >= originalHeight) && originalWidth > 640 && imageRatio >= 1.5) {
				// Querformat
				adjustedWidth = 640;
				adjustedHeight = parseInt(640 / imageRatio);
			} else if(originalHeight > 426 && originalWidth > 640) {
				// Andere Ausreiser
				if(imageRatio >= 1.5) {
					adjustedWidth = 640;
					adjustedHeight = parseInt(640 / imageRatio);
				} else {
					adjustedHeight = 426;
					adjustedWidth = parseInt(426 * imageRatio);
				}
			}
			$(".cms-magazine-article-gallery-articleColumn").addClass("cms-magazine-article-gallery-fullColumn");
			$(".cms-magazine-article-gallery-toolColumn").hide();
		}
		
		$(".cms-magazine-article-gallery-articleColumn").show();		
		img.attr("width",adjustedWidth);
		img.attr("height",adjustedHeight);
		img.show();
		$("#imageCounter").show();
		$("#imageWidth").html(originalWidth);
		$("#galleryTitle").html(galleryObj[current.gallery].galleryTitle);
		$("#galleryImageText").html(nextImage.text);
		//$(".galleryOverlay").css("width","52%");
		$(".imageWidth").css("width",adjustedWidth + "px");
		$(".imageHeight").css("height",adjustedHeight + "px");
		$("#galleryThumb-" + current.gallery + "-" + current.imageInGallery).addClass("galleryThumbActive");
		$("#imageNr").html(current.imageInGallery+1);
		$("#numberOfPictures").html(galleryObj[current.gallery].numberOfPictures);
		location.hash = "gallery=" + galleryObj[current.gallery].id + "&image=" + current.imageInGallery;
		sProp7 += galleryObj[current.gallery].galleryTitle;
		// next line counts pis per picture... de-activated on request of editorial department
		// sProp7 += "_image" + current.imageInGallery;
		if(galleryObj[Gallery].specialTopic != "")
			sProp7 += "_SPECIAL_" + galleryObj[current.gallery].specialTopic;
	} else {
		// not regular image 
		var interstitial = '<h4 id="galleryTitle">Similar galleries</h4>';
		
		// Test 
		// console.log(galleryObj[current.gallery].relatedArticles.length)
		
		if (galleryObj[current.gallery].relatedArticles.length > 0) {
		    var interstitialArticleId = galleryObj[current.gallery].relatedArticles[0].id; 
			var interstitialSearchStr = articleGalleryUrl + "?article=" + interstitialArticleId + "&gal=" + galleryObj[current.gallery].id;
			interstitial += '<div class="interstitialItem interstitialItemSelected"><a href="' + interstitialSearchStr + '"><img src="/' + galleryObj[current.gallery].url + '" /></a><div class="interstitialText"><a href="' + interstitialSearchStr+ '">' + galleryObj[current.gallery].galleryTitle + '</a></div></div>';
		} else {
		
		}
		
		var counter = 0;
		
		for(var i=0; i< galleryObj[current.gallery-1].relatedGalleries.length; i++) {
			if(galleryObj[current.gallery-1].relatedGalleries[i].id != galleryObj[current.gallery].id && counter < 3) {
				//interstitial += '<div class="interstitialItem"><a href="?gallery=' + galleryObj[current.gallery-1].relatedGalleries[i].id + '"><img src="/' + galleryObj[current.gallery-1].relatedGalleries[i].url + '" /></a><div class="interstitialText"><a href="?gallery=' + galleryObj[current.gallery-1].relatedGalleries[i].id + '">' + galleryObj[current.gallery-1].relatedGalleries[i].galleryTitle + '</a></div></div>';
				
				if (galleryObj[i+1].galleryArticles.length > 0) {
				    interstitialArticleId = galleryObj[i+1].galleryArticles[0].id; 
					interstitialSearchStr = articleGalleryUrl + "?article=" + interstitialArticleId + "&gal=" + + galleryObj[current.gallery-1].relatedGalleries[i].id;
					interstitial += '<div class="interstitialItem"><a href="' + interstitialSearchStr + '"><img src="/' + galleryObj[current.gallery-1].relatedGalleries[i].url + '" /></a><div class="interstitialText"><a href="' + interstitialSearchStr +  '">' + galleryObj[current.gallery-1].relatedGalleries[i].galleryTitle + '</a></div></div>';					
				} else {
				}
				
				// Test
				// console.log(galleryObj[i+1].galleryArticles.length)
				
				counter++;
			}
		}
		
		$("#galleryImageContainer").css("background-image","none");
		$(".imageWidth").css("width","570px");
		$(".imageHeight").css("height","380px");
		//$("#galleryTitle").html("Similar galleries");
		$("#galleryImageText").html("");
		$("#relatedGalleries").html(interstitial);
		$(".galleryOverlay").css("width","40px");
		$("#imageCounter").hide();
		$("#relatedGalleries").show();
		$("#galleryPreview" + current.gallery).addClass("galleryPreviewActive");
		sProp7 += "Overview";
	}

	$(".galleryDoLeft").attr("href", current.prevHref );
	$(".galleryDoRight").attr("href", current.nextHref );
	
	// OMNITURE (remove event18, change sProp7 and fire)
	if(s.events.indexOf("event18") > -1) {
		var newEvents = new Array();
		for(var i=0;i<s.events.split(",").length;i++)
			if(s.events.split(",")[i] != "event18")
				newEvents.push(s.events.split(",")[i]);		
		s.events = newEvents.toString();
	}
	if(firstGalleryPicBool) {
		s.events = s.events.toString().replace(/event15,/g, "event18,");
		firstGalleryPicBool = false;
	} else {
		s.events = s.events.toString().replace(/event15,/g, "");
	}
	s.events = s.events.toString().replace(/event12/g, "");
	s.events = s.events.toString().replace(/event13,/g, "");
	s.prop7 = sProp7.toString().replace(/\s/g,'_').replace(/\"/,'_');
	s.eVar0 = "";
	s.eVar1 = "";
	s.eVar11 = "";
	s.eVar16 = "";
	s.eVar22 = "";
	s.eVar25 = "0";
	s.eVar32 = "";
	s.eVar38 = "";
	s.eVar43 = "";
	s.eVar50 = "";
	s.campaign=s.getQueryParam("extcidmnoway");//swe20111116 count all
	var s_code=s.t();      
  	if(s_code) document.write(s_code);		

	// IVW
	if(!onStartBool) {
		var ivwImg = new Image();
		ivwImg.src = $("#ivwpix1").attr("src") +"&d2=" + Date.parse(new Date());

		//var ivwImg2 = new Image();
		//ivwImg2.src = $("#ivwpix2").attr("src") +"&d2=" + Date.parse(new Date());
	}
	
	// ADS mgmt
	// remove div-based adtags
	if(!adtagsIframesBool && !onStartBool) {
		$(".advert-top-head").replaceWith('<iframe id="galleryAdBanner" src="mz_gallery_adtag_banner?ord='+adTagOrd+'&targeting='+adTargeting+'" allowtransparency="true" frameborder="0" width="728" height="95">&nbsp;</iframe>');
		$(".advert-sky-right").replaceWith('<iframe id="galleryAdSky" src="mz_gallery_adtag_sky?ord='+adTagOrd+'&targeting='+adTargeting+'" allowtransparency="true" frameborder="0" width="300" height="605">&nbsp;</iframe>');
		$("#advert-contentad-right").replaceWith('<div class="adtag"><iframe id="galleryAdRectangle" src="mz_gallery_adtag_mediumrectangle?ord='+adTagOrd+'&targeting='+adTargeting+'" allowtransparency="true" frameborder="0" width="300" height="250">&nbsp;</iframe></div>');
		adtagsIframesBool = true;
	} else {
		$("#galleryAdRectangle").attr("src","mz_gallery_adtag_mediumrectangle?ord=" + adTagOrd + "&targeting=" + adTargeting);
		$("#galleryAdBanner").attr("src","mz_gallery_adtag_banner?ord=" + adTagOrd + "&targeting=" + adTargeting);
		$("#galleryAdSky").attr("src","mz_gallery_adtag_sky?ord=" + adTagOrd + "&targeting=" + adTargeting);
	}
	adtagShow("#as24-magazine-rightcol-adtag-1"); 
	adtagShow("#as24-magazine-rightcol-adtag-2"); 		
	adtagHide("#as24-magazine-rightcol-adtag-1"); 
	adtagHide("#as24-magazine-rightcol-adtag-2"); 
		
}
function doRightImage() {
	if (!disableRightBool) {
		if(current.imageInGallery < (galleryObj[current.gallery].pictures.pictures.length - 1))	{
			current.imageInGallery ++;
			current.indexInCarousel ++;
			doChangeImage(current.imageInGallery,current.gallery,false, false);
		} else if(current.gallery < (galleryObj.length-1)) {
			current.gallery ++;
			current.imageInGallery = -1;
			current.indexInCarousel ++;
			doChangeImage(current.imageInGallery,current.gallery,false, false);
		}
	}
	
	// Test 
	// testGallery (current.imageInGallery); 		
	
	return false;
}
function doLeftImage() { 
	if(current.imageInGallery > 0 || (current.imageInGallery > -1 && current.gallery > 0)) {
		current.imageInGallery --;
		current.indexInCarousel --;
		doChangeImage(current.imageInGallery,current.gallery,false, false);
	} else if (current.gallery > 0) {
		current.gallery --;
		current.imageInGallery = galleryObj[current.gallery].pictures.pictures.length -1;
		current.indexInCarousel --;
		doChangeImage(current.imageInGallery,current.gallery,false, false);
	}
	
	// Test 
	// testGallery (current.imageInGallery); 	
	
	return false;
}
/* end - old gallery functions */
function loadGallery(galleryId, onStartBool) {
	$.ajax({
	 	type: "GET",
		beforeSend:function(){
			// show loadIcon 
			// ToDo		
		},				
		url: "as24-magazine-gallery-handler.aspx?gallery=" + galleryId,
		success: function(data){
			var galleryTransObj = eval(data);
			galleryObj = galleryTransObj.galleries;
			current = galleryTransObj.current; 
			
			$(".galleryDoLeft").click(function(e){
				e.preventDefault();
				doLeftImage();
				return false;
			});	
				
			$(".galleryDoRight").click(function(e){
				e.preventDefault();
				doRightImage();
				return false;
			});					
						
			doChangeImage(current.imageInGallery,current.gallery,false, onStartBool);
			galleryLoadedBool = true; 
			
			$(".galleryDoRightIcon").show(); 
	 	}
	});
}
function testGallery(testData) {
	testData = testData + "<br/>" + $(".cms-magazine-gallery-test").html();	
	$(".cms-magazine-gallery-test").html(testData);	
}
$(function() {
	var v = $.parseQuery(); 
	var tempGalParam = v.gal; 	 
	
	// load gallery on start
	if ( typeof(articleGalleryIdStart) != "undefined") {	
		// article page 
		if ((articleGalleryIdStart != 99) && (typeof(tempGalParam) != "undefined")) {
			$("#cms-magazine-article-gallery-container").show();		
			$("#cms-magazine-article-gallery-toolColumn").show();
			$("#cms-magazine-article-toppic-container").hide();
			loadGallery (articleGalleryIdStart, true); 		
			
			galleryLoadedBool = true; 
		} else {
			// check for caching bug 
			if (typeof(tempGalParam) != "undefined") {
				loadGallery (tempGalParam, true); 		
				$("#cms-magazine-article-gallery-container").show();		
				$("#cms-magazine-article-gallery-toolColumn").show();
				$("#cms-magazine-article-toppic-container").hide();
			} else {
				$("#cms-magazine-article-gallery-container").hide();		
				$("#cms-magazine-article-toppic-container").show();		
			}
		}
	} else {
		// no article page 
	}
	
	$(".loadGallery").click(function() {
		// Test 
		// console.log("galleryLoadedBool: " + galleryLoadedBool); 
	
		if (!galleryLoadedBool) {
			$("#cms-magazine-article-gallery-container").show();	
			$("#cms-magazine-article-gallery-toolColumn").show();
			$("#cms-magazine-article-toppic-container").hide();
			loadGallery (articleGalleryId, false); 
			location.href="#top";
			
			galleryLoadedBool = true; 
		} else		{
			location.href="#top";
		}
		return false; 
	}); 
	
});
