﻿var counter = 0;
var myInterval = 0;
var showingPictureAofB;
$(function (){
		$("#onloadJSON").val($("input[id$=BoxCoordinates]").val());
		VD.MapCreator.init();
	});
	// vd namespace
	var VD = {};
	VD.MapCreator = {
		onStopDrag:function (){
		var str = "";
			$("#BackgroundImg .danmarksKortForegroundItem").each(function (i,item){
				// create JSON repres. of data
				str += ",{id:'" + item.id + "',x:"+ parseInt(item.style.left) + ",y:"+ parseInt(item.style.top) + "}";
			});
			str = "["+str.substring(1) +"]";
			$("input[name*=BoxCoordinates_PH]").val(str);
		},
		onStartDrag:function (){
			
		},
		init:function (){
			$("div[rel=drag_false] .danmarksKortForegroundItem[rel=published_false]").hide();
			// parse JSON data from hidden field
			var boxes = eval($("input[id$=BoxCoordinates]").val());
			$(boxes).each(function (i,item){
				$("#"+item.id).css({position:"absolute",left:item.x+"px",top:item.y+"px"});
			});
			$("#BackgroundImg div[rel=drag_true] .danmarksKortForegroundItem").draggable({containment:$("#BackgroundImg"),start:VD.MapCreator.onStartDrag,stop:VD.MapCreator.onStopDrag});
		}
	};
	VD.SlideShow = {
		nextPhoto:function(){
			$("#next").click(function(){
				var imgs = $(".slideShow .imgBox");
				var current = imgs[counter];
				$(current).fadeOut("slow");
				if(counter < imgs.length-1){
					counter += 1;
				}
				else {
					counter = 0;
				}
				var current = imgs[counter];
				$(current).fadeIn("slow");
				VD.SlideShow.showingPicAofB();
				//var hej = "#"+$("#imgcount").innerHTML+"#";
				//$("#imgcount").append(hej);
				
				clearInterval(myInterval);
				
			});
		},
		prevPhoto:function(){
			$("#prev").click(function(){
				var imgs = $(".slideShow .imgBox");
				var current = imgs[counter];
				$(current).fadeOut("slow");
				if(counter == 0){
					counter = imgs.length-1;
				}
				else {
					counter -= 1;
				}
				var current = imgs[counter];
				$(current).fadeIn("slow");
				VD.SlideShow.showingPicAofB();
				clearInterval(myInterval);
				
			});
		},
		myPlay:function(state){
			if(state == "start"){
				var imgs = $(".slideShow .imgBox");
				var current = imgs[counter];
				$(current).fadeOut("slow");
				if(counter < imgs.length-1){
					counter += 1;
				}
				else {
					counter = 0;
				}
				var current = imgs[counter];
				$(current).fadeIn("slow");
				VD.SlideShow.showingPicAofB();
				}
				else {
					clearInterval(myInterval);
				}
			},
		playShow:function(){
		//rbm sat rotation igang ved start 16-05-2011
		    myInterval = setInterval("VD.SlideShow.myPlay('start')",3000);
			$("#play").toggle(function(){
				myInterval = setInterval("VD.SlideShow.myPlay('start')",3000);
				//$("#play").empty().append("Stop");
			},
			function(){
				myInterval = setInterval("VD.SlideShow.myPlay('start')",3000);
				clearInterval(myInterval);
				//$("#play").empty().append("Play");
			});
		},
		showingPicAofB:function(){
			var imgs = $(".slideShow .imgBox");
			imgNo = counter+1;
			$("#imgcount").empty().append(showingPictureAofB.replace("{0}", imgNo).replace("{1}",imgs.length) ); 
		
		},
		init:function(){
			VD.SlideShow.prevPhoto();
			VD.SlideShow.nextPhoto();
			VD.SlideShow.playShow();
			showingPictureAofB = $("#imgcount").text();
			VD.SlideShow.showingPicAofB();
		}
	};
	VD.TabNavigation = {
			toggleTabs:function(){
				$("#tabs a[class^=tab]").click(function(){
					var tabNo = $(this).attr("class").split("tab")[1];
					$(".tabContent.show").removeClass("show");
					$("#content"+tabNo).addClass("show");
					$("#tabs li.selected").removeClass("selected");
					$(this).parent().addClass("selected");
					setProductPageTabIndex( $(this).parent().attr("id") );
				});
			}
		}
$(document).ready(function(){
$("html").addClass("js");
VD.SlideShow.init();
VD.TabNavigation.toggleTabs();
})
