/*
Picture collection component
*/
var PictureCollectionCompInstances = new Array();
function PictureCollectionComp(){
    this.id = null;
	this.PictureColl = [];
	this.CommentColl = [];
	this.OpenInPopup = false;
	this.index = 0;
	this.Pages = [];
	this.PageIndex = 0;
	this.thumbnails = false;
	this.slides = false;
	this.fadeTimer = null;
    
    this.slideTimer = 2000;
	PictureCollectionCompInstances.push(this);
};

PictureCollectionComp.prototype.Init = function(){
	//find all picture collection components
	var pictureComp = document.getElementById(this.id);
        if(pictureComp != null){
		//ok we have a picture collection on the page!
		var picture, thumbnailAnchors, anchorsClassName;
		var pictureArr = new Array();
		//attach thumbnail events
        thumbnailAnchors = pictureComp.getElementsByTagName("*");
        for(var x=0;x<thumbnailAnchors.length;x++){
			anchorsClassName = thumbnailAnchors[x].className;
			
			if(/thumbnail-links/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.LoadImage;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
			}
			//attach prev event for pic-collection
			if(/picture-collection-prev/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.Prev;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
			}
			//attach next event for pic-collection
			if(/picture-collection-next/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.Next;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
			}
		
			//attach next event for thumb
			if(/picture-collection-thumb-next/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.PagerNext;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
				
			}
			
			//attach prev event for thumb
			if(/picture-collection-thumb-prev/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.PagerPrev;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
			}
			
			//attach event for click on specific page
			if(/picture-collection-pager-link/i.test(anchorsClassName)){
				thumbnailAnchors[x].onclick = this.SelectPage;
				thumbnailAnchors[x].setAttribute("obj", "pictureColl" + this.id);
			}
			
			//attach pause function to slides
			if(/slide-image/i.test(anchorsClassName)){
				thumbnailAnchors[x].onmouseover = function(){
					//alert("testar");
				}
			}
		
		}
		
		//if thumbnail variant
		if(this.thumbnails)
			this.PagerInit(this);
			
		//if slide variant
		if(this.slides)
            this.StartSlideShow();

    }
};
PictureCollectionComp.prototype.Next = function(){
	var compObj;
	if(this.nodeName != null)
		compObj = eval(this.getAttribute("obj"));
	else
		compObj = this;
		
	compObj.index++;
   
	if(compObj.index > (compObj.PictureColl.length-1))
		compObj.index = 0;
	
	compObj.LoadImage();
	//if slide variant also check that we don't do this on next prev click
	if(compObj.slides){
		if(this.nodeName == null){
			var time = parseInt(eval("pictureColl" + compObj.id + ".slideTimer"));
            clearTimeout(this.fadeTimer);
            this.fadeTimer = setTimeout("pictureColl" + compObj.id + ".Next()",time);
	    }
    }	
	return false;
};
PictureCollectionComp.prototype.Prev = function(){
	var compObj = eval(this.getAttribute("obj"));
	compObj.index--;
	if(compObj.index < 0)
		compObj.index = (compObj.PictureColl.length-1);
	
	compObj.LoadImage();
	return false;
};
PictureCollectionComp.prototype.LoadImage = function(){
	var compObj;
	if(this.nodeName != null){
		compObj = eval(this.getAttribute("obj"));
		var temp;
		temp = this.id.substring(this.id.indexOf("-")+1);
		compObj.index = (temp-1);
	}
	else{
		compObj = this;
	}

	var bigImage = document.getElementById(compObj.id).getElementsByTagName("img");
	bigImage[0].src = compObj.PictureColl[compObj.index];
	bigImage[0].removeAttribute("height");
	bigImage[0].removeAttribute("width");
	var comment = document.getElementById(compObj.id).getElementsByTagName("h4");
	comment[0].innerHTML = compObj.CommentColl[compObj.index];
	
	var imageIndex = document.getElementById(compObj.id).getElementsByTagName("span");
	imageIndex[0].innerHTML = compObj.index + 1;
	
	//if variant thumbnails select thumbnail
	if(compObj.thumbnails){
		//itterate through links whithin this instance and set default class
		var links = document.getElementById(compObj.id).getElementsByTagName("a");
		for(var i=0;i<links.length;i++){
			if(/thumbnail-links/i.test(links[i].className)){	
				links[i].className = "thumbnail-links";
			}
		}
		document.getElementById(compObj.id + "-" + (compObj.index +1)).className = "thumbnail-links thumbnail-selected";
	
		//show correct thumbnail page!
		var currentPage = (compObj.index)/10;
		currentPage = Math.floor(currentPage);
		if(currentPage > compObj.PageIndex){
			//are we on the first page and user pressed prev picture?
			if(currentPage == (compObj.Pages.length-1) || compObj.PageIndex == 0){
				compObj.PageIndex = 3;
				compObj.PagerPrev();
			}
			else{
				compObj.PagerNext();
			}
		}
		if(currentPage < compObj.PageIndex){
			if(currentPage == 0)
				compObj.PageIndex = 0;
			compObj.PagerPrev();
		}
	}
	
	return false;
};
PictureCollectionComp.prototype.PagerInit = function(obj){
	var pages = document.getElementById(obj.id).getElementsByTagName("ul");
	var pagesClassName;
	for(var i=0;i<pages.length;i++){
		pagesClassName = pages[i].className;
		if(/picture-collection-thumbnail-page/i.test(pagesClassName)){
			this.Pages[this.Pages.length] = pages[i];
		}
	}
};
PictureCollectionComp.prototype.PagerNext = function(){
	var compObj;
	if(this.nodeName != null)
		compObj = eval(this.getAttribute("obj"));
	else
		compObj = this;
		
	compObj.PageIndex++;
	if(compObj.PageIndex > (compObj.Pages.length-1))
		compObj.PageIndex = compObj.Pages.length-1;

	for(var i=0;i<compObj.Pages.length;i++){
		compObj.Pages[i].className = "picture-collection-thumbnail-page";
	}

	compObj.Pages[compObj.PageIndex].className = "picture-collection-thumbnail-page page-visible";
	return false;
};
PictureCollectionComp.prototype.PagerPrev = function(){
	var compObj;
	if(this.nodeName != null)
		compObj = eval(this.getAttribute("obj"));
	else
		compObj = this;
		
	compObj.PageIndex--;
	if(compObj.PageIndex < 0)
		compObj.PageIndex = 0;

	for(var i=0;i<compObj.Pages.length;i++){
		compObj.Pages[i].className = "picture-collection-thumbnail-page";
	}
	
	compObj.Pages[compObj.PageIndex].className = "picture-collection-thumbnail-page page-visible";
	return false;
};
PictureCollectionComp.prototype.SelectPage = function(){
	var compObj = eval(this.getAttribute("obj"));
		
	compObj.PageIndex = (this.innerHTML-1);
	
	for(var i=0;i<compObj.Pages.length;i++){
		compObj.Pages[i].className = "picture-collection-thumbnail-page";
	}
	
	//select current page
	var pageAnchors = document.getElementById(compObj.id).getElementsByTagName("a");
	for(var i=0;i<pageAnchors.length;i++){
		if(/picture-collection-pager-link/i.test(pageAnchors[i].className)){
			pageAnchors[i].className = "picture-collection-pager-link";
		}
	}
	
	this.className = "picture-collection-pager-link page-selected";
	compObj.Pages[compObj.PageIndex].className = "picture-collection-thumbnail-page page-visible";
	return false;
};
PictureCollectionComp.prototype.StartSlideShow = function(){
    var time = parseInt(eval("pictureColl" + this.id + ".slideTimer"));
    clearTimeout(this.fadeTimer);
    this.fadeTimer = setTimeout("pictureColl" + this.id + ".Next()",time);
};
PictureCollectionComp.prototype.PauseSlideShow = function(){
	clearTimeout(this.fadeTimer);
};
function RunPictureCollInits(){
	for(var i=0;i<PictureCollectionCompInstances.length;i++){
		PictureCollectionCompInstances[i].Init();
	}
};

SafeOnload.AddFunction(RunPictureCollInits);

