var slideshow = {
		
		currentSlideShowBox: "",
		
		
		galleryPaging: function (gallery, pagenumber)
		{
	
			var itemsPerPage = Number(gallery.attr("itemsx")) * Number(gallery.attr("itemy"));

			gallery.find(".page-button").attr("src", "/images/editor/gallery-page.png");
			gallery.find("#"+pagenumber).attr("src", "/images/editor/gallery-page-selected.png");
			
			gallery.children().each(function(xx) {				
				var child = $(this);
				if (child.attr("class") != "paginator"){
					if ( (xx >= (itemsPerPage)*(pagenumber-1)) && (xx < (itemsPerPage)*pagenumber)) {
						child.css("display", "inline")
					} else {
						child.css("display", "none")
					}

					
					
				}
			
			})
		},
		
		
		initGallery: function (gallery)
		{
	
			var itemsPerPage = Number(gallery.attr("itemsx")) * Number(gallery.attr("itemy"));
			
			var zz = 0;
			
			gallery.children().each(function(xx) {				
				var child = $(this);
				child.attr("id", xx);
				if (child.attr("class") != "paginator"){
					
					if ($.browser.msie) {
						child.find(".info-section").hide();
					}
					
					zz = zz+1;

					if (zz == Number(gallery.attr("itemsx"))){
						zz = 0;
						child.css("margin-right", "0px")
					}
					
					child.click(function() {
						slideshow.currentSlideShowBox = $(this).closest(".GalleryBox");
						slideshow.openFullScreen($(this).attr("id"))
					})	
					
					child.hover(function(){
						$(this).find(".info-section").css("opacity", 1);
						if ($.browser.msie) {
							$(this).find(".info-section").show();
						}
					},
					function(){
						$(this).find(".info-section").css("opacity", 0);
						if ($.browser.msie) {
							$(this).find(".info-section").hide();
						}
					})
				}
			})
			
			if (itemsPerPage < gallery.children().length){  // if paging is needed
					
				
				gallery.append("<div class='paginator'><div class='inner'></div></div>");				
				var numberOfPages = Math.floor(gallery.children().length/itemsPerPage)

				for (i=1;i<=numberOfPages;i++)
				{
					gallery.find(".paginator").find(".inner").append("<img class='page-button' src='/images/editor/gallery-page.png' id='"+i+"'>")
				}
				
				gallery.find(".page-button").click(function() {
					slideshow.galleryPaging(gallery, Number($(this).attr("id")))
					//alert(Number($(this).attr("id")))
				})
			}
			
			
			
			
			
			var itemsPerPage = Number(gallery.attr("itemsx")) * Number(gallery.attr("itemy"));


		},
				
		
		init: function ()
		{
			
			//if window is smaller than page - scroll regulary.
			if ( $(".page").height() >  $(window).height()){
					$(".page").css("top", "0px");
					$(".page").css("margin-top", "0px")
			}
			
			//injecting actions
			var i = 0;
			
			//CSS
			
			
			//SLIDESHOW
			
			$(".SlideShowBox").each(function() {

				slideshow.initSlideShow($(this), true);
			})
			
			//PICTURE
			$(".PictureBox").each(function() {
				slideshow.initPicture($(this));
			})
			
			
			
			//CONTACT
			
			$(".ContactFormBox").each(function() {

				contact.initContact($(this));
			})
			
			
			
			
			//GALLERY
			
			$(".GalleryBox").each(function() {
				var gallery = $(this)
				slideshow.initGallery(gallery)
				slideshow.galleryPaging(gallery, 1);
			})
			
			


		},

		
		
		initPicture: function(picturePointer) {
			
			if ($.browser.msie) {
				picturePointer.find(".info-section").hide();
			}
			
			//console.log("initing social like")
			picturePointer.hover(function(){
				
				
				
				//like
							if (($(this).attr("social") == "true") && ($(this).width() > 150)) {
								

									var likeTitle = $(this).find(".info-title").text()
									var likeDesc = $(this).find(".description").text()
									
									if (likeTitle == "") {
										likeTitle = $('meta[name=title]').attr("content");	
									}
									
									if (likeDesc == "") {
										likeDesc = $('meta[name=description]').attr("content");	
									}
									
									var likePic = $(this).find("img").attr("src")
									
									if (likeTitle.length > 30){
										likeTitle = likeTitle.substring(0,30)+"..."
									}
									
									
									if (likeDesc.length > 30){
										likeDesc = likeTitle.substring(0,30)+"..."
									}
									
									likePic = likePic.replace("http://", "");
									likePic = encodeURIComponent(likePic);
									likeTitle = encodeURIComponent(likeTitle);
									likeDesc = encodeURIComponent(likeDesc);
									var likeVbid = $("body").attr("vbid");
									var likeDomain = $("body").attr("domain");
									
									
									var likeLink = likeDomain+"/like?var="+likeVbid+"^"+likeDesc+"^"+likeTitle+"^"+likePic;  //"+"&pic="+this.vcImageUrl+"&title="+"yo dudes whats up?";
									//console.log(likeLink)
									$(this).append("<div class='like-container' style='position:absolute; width:200px; height:60px; z-index:9999999999; top:10px; left:10px; overflow:hidden;'><iframe src='//www.facebook.com/plugins/like.php?href="+likeLink+"&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21&amp;' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:450px; height:21px;' allowTransparency='true'></iframe></div>");
							
							}
							
							//}
							
									
									
							/**
							 End of dynamic like test
							*/

				//end of like
				
				
				$(this).find(".info-section").css("opacity", 1);	
				if ($.browser.msie) {
					$(this).find(".info-section").show();
				}
			},
			function(){
				$(this).find(".info-section").css("opacity", 0);
				if ($.browser.msie) {
					$(this).find(".info-section").hide();
				}
				
				$(this).find('.like-container').remove();
				
				
				
				//$(this).find(".info-section").css("filter","alpha(opacity=0)");
			})
		},
			
		
			
		
		
		
		initSlideShow: function(slideshowPointer, startAtZero) {
			
			slideshowPointer.find(".PictureBox").first().css("z-index", 999);
			
			if (startAtZero == true){
				slideshowPointer.attr("currentSlide", 0);
			}
			
			
			slideshowPointer.append("<div class='paginator'><div class='inner'><img class='background' src='/images/editor/slideshow-paginator.png' USEMAP='#slideshow-paginatora' /><span class='text'>1/1</span><a class='prev-button button' href='javascript:void(0);'></a><a class='next-button button' href='javascript:void(0);'></a></div></div>" )
			
			slideshowPointer.find(".next-button").click(function() {
				slideshow.currentSlideShowBox = $(this).closest(".SlideShowBox");
				slideshow.StopAutoPlay(slideshowPointer); //stop autoplay
				slideshow.nextSlide();
			})
			
			slideshowPointer.find(".prev-button").click(function() {
				slideshow.currentSlideShowBox = $(this).closest(".SlideShowBox");
				slideshow.StopAutoPlay(slideshowPointer); // stop autoplay
				slideshow.prevSlide();
			})
			
			//actions
			
			slideshowPointer.find(".paginator").css("opacity", 0);
			
			slideshowPointer.hover(function(){
				$(this).find(".paginator").css("opacity", 1);
				$(this).find(".info-section").css("opacity", 1);
				if ($.browser.msie) {
					$(this).find(".info-section").show();
				}
				
				
				//console.log("like log", $(this).attr("social"))
				
				//like
				if (($(this).attr("social") == "true") && ($(this).width() > 150) ) {
					

						var likeTitle = $(this).find(".info-title").text()
						var likeDesc = $(this).find(".description").text()
						
						if (likeTitle == "") {
							likeTitle = $('meta[name=title]').attr("content");	
						}
						
						if (likeDesc == "") {
							likeDesc = $('meta[name=description]').attr("content");	
						}
						
						var likePic = $(this).find("img").attr("src")
						
						if (likeTitle.length > 30){
							likeTitle = likeTitle.substring(0,30)+"..."
						}
						
						
						if (likeDesc.length > 30){
							likeDesc = likeTitle.substring(0,30)+"..."
						}
						
						likePic = likePic.replace("http://", "");
						likePic = encodeURIComponent(likePic);
						likeTitle = encodeURIComponent(likeTitle);
						likeDesc = encodeURIComponent(likeDesc);
						var likeVbid = $("body").attr("vbid");
						var likeDomain = $("body").attr("domain");
						
						
						var likeLink = likeDomain+"/like?var="+likeVbid+"^"+likeDesc+"^"+likeTitle+"^"+likePic;  //"+"&pic="+this.vcImageUrl+"&title="+"yo dudes whats up?";
						//console.log(likeLink)
						$(this).find(".like-container").remove();
						$(this).find(".paginator").append("<div class='like-container' style='position:absolute; width:200px; height:60px; z-index:9999999999; top:7px; left:13px; overflow:hidden;'><iframe src='//www.facebook.com/plugins/like.php?href="+likeLink+"&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21&amp;' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:450px; height:21px;' allowTransparency='true'></iframe></div>");
				
				}
				
				//}
				
						
						
				/**
				 End of dynamic like test
				*/

	//end of like
	
	
				
			},
			function(){
				$(this).find(".paginator").css("opacity", 0);
				$(this).find(".info-section").css("opacity", 0);
				if ($.browser.msie) {
					$(this).find(".info-section").hide();
				}
			})
			
			
			
			
			//for each slide
			
			
			slideshowPointer.children().each(function() {
			
				var child = $(this);
				
				
				if ($.browser.msie) {
					child.find(".info-section").hide();
				}
				
				
				if (child.attr("class") != "paginator"){
					
					child.css("position", "absolute");
					child.css("top", "0px");
					child.css("left", "0px");
					
					
					child.click(function() {
						slideshow.currentSlideShowBox = $(this).closest(".SlideShowBox");
					//	slideshow.openFullScreen()
					})
					
					
					//TOUCH
					
					var down_x = null;
					var up_x = null;
					
					child.mousedown(function(e){
						slideshow.currentSlideShowBox = $(this).closest(".SlideShowBox");
						e.preventDefault();
					    down_x = e.pageX;
					  });
						
					child.mouseup(function(e){
					    up_x = e.pageX;
					    do_work();
					  });
						
					
					child.bind('touchstart', function(e){
					    down_x = e.originalEvent.touches[0].pageX;
					  });
						
					child.bind('touchmove', function(e){
					    e.preventDefault();
					    up_x = e.originalEvent.touches[0].pageX;
					  });
						
					child.bind('touchend', function(e){
						slideshow.currentSlideShowBox = $(this).closest(".SlideShowBox");
					    do_work();
					  });

					function do_work()
					{
					//console.log("DO WORK!");
					  if ((down_x - up_x) > 50)
					    {
						  slideshow.nextSlide();
					    } else {
					    	if ((up_x - down_x) > 50)
						    {
						    	 slideshow.prevSlide();
						    } else {
						    	if ($(".lightbox").children().length != 0){
						    		
						    		$(".lightbox").remove();
						    	} else {
						    		//console.log("NO lightbox");
						    		slideshow.openFullScreen()
						    	}

						    }
					    
					    }
					}
							
					
					
					
					
					
					
					
					
					
				}
			
			})
			
						
				slideshow.currentSlideShowBox = slideshowPointer;
				slideshow.showSlide();
			
			
			   var autoplayFlag = slideshow.currentSlideShowBox.attr("autoplayf");
			   //alert(autoplayFlag);
			   
			   if (autoplayFlag == "false") {
				  
				//do nothing	   
			   } else {
				   slideshow.StartAutoPlay(slideshowPointer)
			   }
			   
			
			
			
		},
		
		showSlide : function()
		{
			
			var i=0;
			slideshow.currentSlideShowBox.children().each(function() {
			
			paginatorText = (Number(slideshow.currentSlideShowBox.attr("currentSlide"))+1) + "/" + (slideshow.currentSlideShowBox.children().length-1)
			slideshow.currentSlideShowBox.find(".text").text(paginatorText)
				
				var child = $(this);
				if (child.attr("class") != "paginator"){
			
										
					var nextSlideNum = Number(child.closest(".SlideShowBox").attr("currentSlide"))+1
					var prevSlideNum = Number(child.closest(".SlideShowBox").attr("currentSlide"))-1
				
					
					
					
					if (prevSlideNum == -1) {
						prevSlideNum = slideshow.currentSlideShowBox.children().length-2
					}
					
					if (nextSlideNum == slideshow.currentSlideShowBox.children().length-1){
						nextSlideNum = 0;
					}
					
					
					//console.log("These are the 3: ", prevSlideNum,child.closest(".SlideShowBox").attr("currentSlide"), nextSlideNum)
					
					if ( String(i) ==  child.closest(".SlideShowBox").attr("currentSlide")){
						var originalSrc = child.find("img").attr("originalSrc");
						child.css("opacity", 1)
						if (child.find("img").attr("src") == ""){
							child.find("img").attr("src", originalSrc)
						}
						child.show();
					}
					
					
					
					else if ( Number(i) == nextSlideNum ) {
							var originalSrc = child.find("img").attr("originalSrc");
							child.css("opacity", 0)
							if ($.browser.msie) {
								child.hide();
							}
							//child.find("img").css("opacity", 1)
							if (child.find("img").attr("src") == ""){
								child.find("img").attr("src", originalSrc)
							}
							child.show();
							if ($.browser.msie) {
								child.hide();
							}
					} 
					
					else if ( Number(i) == prevSlideNum) {
						var originalSrc = child.find("img").attr("originalSrc");
						child.css("opacity", 0)
						if ($.browser.msie) {
								child.hide();
							}
						//child.find("img").css("opacity", 1)
						if (child.find("img").attr("src") == ""){
							child.find("img").attr("src", originalSrc)
						}						
						
						child.show();	
						if ($.browser.msie) {
							child.hide();
						}
					
					} else {
						
						//child.find("img").attr("src", "")
						child.hide();
						
					}
					
					
					
					
					//else {
					//	
					//	child.find("img").attr("src", "")
					//	child.hide();
					//}
					
					i = i+1;
				}
			
			})
		},
		
		StartAutoPlay: function (slideShowPointer)
		{
			delay = Number(slideShowPointer.attr("delay"))*1000;
			slideShowPointer.autoPlayInterval = setInterval(function () { slideshow.currentSlideShowBox = slideShowPointer; slideshow.nextSlide(); }, delay);
		},

		/**
		Stops autoplay temporarily.
		*/
		StopAutoPlay: function (slideShowPointer)
		{
				clearInterval(slideShowPointer.autoPlayInterval)
		},

		
		
		nextSlide : function ()
		{
			var currSlide = Number(slideshow.currentSlideShowBox.attr("currentSlide"));
			currSlide  = currSlide + 1;
			if (currSlide >= (slideshow.currentSlideShowBox.children().length - 1)){
				currSlide = 0;
			}
			slideshow.currentSlideShowBox.attr("currentSlide", currSlide);
			slideshow.showSlide();
			
		},
		
		prevSlide : function ()
		{
			var currSlide = Number(slideshow.currentSlideShowBox.attr("currentSlide"));
			currSlide  = currSlide - 1;
			if (currSlide < 0){
				currSlide = (slideshow.currentSlideShowBox.children().length - 2);
			}
			slideshow.currentSlideShowBox.attr("currentSlide", currSlide);
			slideshow.showSlide();
			
		},
		
		
		openFullScreen : function (firstSlide)
		{
			
			
			$("body").append("<div class='lightbox' style='position:fixed; top:0px; left:0px; z-index:9999999999999; background-color:black; width:100%; height:100%;'></div>")
			
			
			var fullscreenDom = slideshow.currentSlideShowBox.clone( false, false );
		
			fullscreenDom.find(".paginator").remove();
			fullscreenDom.attr("class", "SlideShowBox")
			
			if (firstSlide == null){
				//fullscreenDom.attr("currentSlide", "0")
			} else{
				fullscreenDom.attr("currentSlide", firstSlide)
			}
			
			
			fullscreenDom.attr("autoplayf", "false");
			
			fullscreenDom.css("width", "100%");
			fullscreenDom.css("height", "100%");
			
			fullscreenDom.css("position", "absolute");
			fullscreenDom.css("top", "0px");
			fullscreenDom.css("left", "0px");
			
			var widthh = $(".lightbox").width();
			var heightt = $(".lightbox").height();
			
			slideshow.resizePic(fullscreenDom, widthh, heightt);
			slideshow.initSlideShow(fullscreenDom, false)
			
			$("body").find(".lightbox").append(fullscreenDom);
		},
		
		
		resizePic : function (fullscreenDom, widthh, heightt )
		{
			
			fullscreenDom.children().each(function() {
				var child = $(this);
				if (child.attr("class") != "paginator"){
					
					child.unbind("click");
					
					var ratio = 1;

					child.css("text-align", "center");
					
					
					ratio1 = heightt*0.9 / Number(child.find("img").height());
					ratio2 = widthh*0.9 / Number(child.find("img").width());

					if (ratio1>ratio2){
						ratio = ratio2
					} else {
						ratio = ratio1
					}
					
					
					
					
					//console.log(widthh, ratio, child.find(".PictureBox").width());
					
					childWidth = child.find("img").width();
					childHeight = child.find("img").height();
					
					newChildWidth = (childWidth * ratio);
					newChildHeight = (childHeight * ratio);
					
					var originalPicHeight = Number(child.find("img").attr("realHeight"));
					var originalPicWidth = Number(child.find("img").attr("realWidth"));
					
					if ((newChildWidth > originalPicWidth) || (newChildHeight > originalPicHeight)){
						newChildWidth = originalPicWidth;
						newChildHeight = originalPicHeight;
					}
					
					//console.log(newChildWidth, newChildHeight)
					
					child.find("img").css("width", newChildWidth+"px");
					child.find("img").css("height", newChildHeight+"px");
					
					
					
					childUrl = child.find("img").attr("src");
					
					var PicFromHighP = childUrl.indexOf("=s");
					
					childUrl = childUrl.split("=s");
					
					var sizeForServe = 0;
					if (newChildWidth > newChildHeight){
						sizeForServe = Math.round(newChildWidth)
					} else {
						sizeForServe = Math.round(newChildHeight)
					}
					
					if (PicFromHighP != -1){
						
					childUrl = childUrl[0]+("=s"+sizeForServe);
					} else {
					childUrl = 	childUrl[0];
					}
					
					child.find("img").attr("src", "" );
					child.find("img").attr("originalSrc", childUrl );
					
					
					child.css("width", widthh);
					child.css("height", heightt);
					
					child.find(".inner-visible").css("width", widthh);
					child.find(".inner-visible").css("height", heightt);
					
					var VertCentering = (heightt - newChildHeight) / 2
					
					child.find("img").css("position", "relative");
					child.find("img").css("top", VertCentering+"px");
					child.find("img").css("left", "0px");
					
				}
			
			})
				
		}

}
