/* Javascript for teknikensvarld.se */

/*
Picture collection component
*/
var PictureCollectionCompInstances = new Array();
function PictureCollectionComp(){
    this.id = null;
	this.PictureColl = [];
	this.CommentColl = [];
	this.CommentColl2 = [];
	this.origImgPath = [];
	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[3].src = compObj.PictureColl[compObj.index];
	bigImage[3].removeAttribute("height");
	bigImage[3].removeAttribute("width");
	var comment = document.getElementById(compObj.id).getElementsByTagName("h4");
	var comment1 = document.getElementById(compObj.id).getElementsByTagName("h3");
	document.getElementById("transparentTxtLayer").style.filter = 'alpha(opacity=100)';
	if (compObj.CommentColl[compObj.index] == '') {
		comment[0].innerHTML = '';
		document.getElementById("closeOpenBtn").style.visibility = 'hidden';
		document.getElementById("closeOpenBg").style.visibility = 'hidden';
	}
	else {
		comment[0].innerHTML = compObj.CommentColl[compObj.index];
		document.getElementById("closeOpenBtn").style.visibility = 'visible';
		document.getElementById("closeOpenBg").style.visibility = 'visible';
	}
	document.getElementById("transparentTxtLayer").style.filter = 'alpha(opacity=35)';
	comment1[0].innerHTML = compObj.CommentColl[compObj.index];
	
	document.getElementById('visiblePicCollTxt').style.display = 'inline';
	document.getElementById('hiddenPicCollTxt').style.display = 'inline';
	document.getElementById('closeOpenBtn').style.backgroundImage = 'url(/img/piccollection0809/btn-close-text.gif)';
	
	var origimg = document.getElementById(compObj.id).getElementsByTagName("a");
	origimg[3].href = "/view-image.xml?__xsl=/templates/view-image.xsl&pic=" + compObj.origImgPath[compObj.index];
	document.getElementById("zoomPicColl").href = "/view-image.xml?__xsl=/templates/view-image.xsl&pic=" + compObj.origImgPath[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();
	}
};

function viewHidePicCollTxt() {
	if (document.getElementById('visiblePicCollTxt').style.display == 'none') {
		document.getElementById('visiblePicCollTxt').style.display = 'inline';
		document.getElementById('hiddenPicCollTxt').style.display = 'inline';
		document.getElementById('closeOpenBtn').style.backgroundImage = 'url(/img/piccollection0809/btn-close-text.gif)';
	}
	else {
		document.getElementById('visiblePicCollTxt').style.display = 'none';
		document.getElementById('hiddenPicCollTxt').style.display = 'none';
		document.getElementById('closeOpenBtn').style.backgroundImage = 'url(/img/piccollection0809/btn-open-text.gif)';
	}
}




//Call popup window
function popVdack(url) {
	width = 1010;
	height = 700;
	leftPos = (screen.width) ? (screen.width-width)/2 : 0;
	topPos = (screen.height) ? (screen.height-height)/2 : 0;
	window.open( url, "vinterdack", "resizable=1,scrollbars=1,height="+height+",width="+width+",left="+leftPos+",top="+topPos);
}

function popWinTV(url,iName,iW,iH) {
	width = iW;
	height = iH;
	leftPos = (screen.width) ? (screen.width-width)/2 : 0;
	topPos = (screen.height) ? (screen.height-height)/2 : 0;
	window.open( url, iName, "resizable=1,scrollbars=1,height="+height+",width="+width+",left="+leftPos+",top="+topPos);
}

//Call popup window and resize it to screen size minus chosen width/lenght in pixels
function popTotalScreen(url,brwinname) {
	width = screen.availWidth;
	height = screen.availHeight;
	leftPos = (screen.width) ? (screen.width-width)/2 : 0;
	topPos = (screen.height) ? (screen.height-height)/2 : 0;
	window.open( url, brwinname, "resizable=1,scrollbars=0,height="+height+",width="+width+",left="+leftPos+",top="+topPos);
}

//Check email address for new newsletter subscribers
function chkNewsletterEmail() {
	if (document.SubscriberForm.pf_Email.value == "") {
		alert("Du måste ange en e-postadress!");
		document.SubscriberForm.pf_Email.focus();
		return false;
	}
	pf_Email=document.SubscriberForm.pf_Email.value
	at=pf_Email.indexOf("@");
	lastat=pf_Email.lastIndexOf("@");
	dot=pf_Email.lastIndexOf(".");
	if (at<1||at!=lastat||dot<at) {
		alert("Du har angett en ogiltig e-postadress!");
		return false;
	}
	if (document.SubscriberForm.pf_Email.value.length > 255) {
		alert("E-postadressen innehåller för många tecken!");
		document.SubscriberForm.pf_Email.focus();
		return false;
	}
	if (document.SubscriberForm.pf_Email.value.length < 7) {
		alert("E-postadressen innehåller för få tecken!");
		document.SubscriberForm.pf_Email.focus();
		return false;
	}
}

//Funtion for positioning the layer with latest number
function positionIt() {
	if (showNewnumberlayer == true) {
		thiswidth = document.body.scrollWidth;
		thisheight = document.body.scrollHeight;
		innerwidth = document.documentElement.clientWidth;
		innerheight = document.documentElement.clientHeight;
		
		if (navigator.appName == "Microsoft Internet Explorer") {
			scrollPosition = document.documentElement.scrollTop;
		}
		else {
			scrollPosition = window.pageYOffset;
		}
		
		//Resize opacity layer
		document.getElementById('opacity-Container').style.width = thiswidth + 'px';
		if (thisheight < innerheight) {
			document.getElementById('opacity-Container').style.height = innerheight + 'px';
		}
		else {
			document.getElementById('opacity-Container').style.height = thisheight + 'px';
		}
		
		//Position the order layer
		if (innerwidth/2-328/2 > 442) {
			innerwidth = 1212;
		}
		document.getElementById('newnumberLayer').style.left = innerwidth/2-328/2 + 'px';
		document.getElementById('newnumberLayer').style.top = scrollPosition+innerheight/2-461/2 + 'px';
		
		//Set opacity and order layer to visible
		document.getElementById('opacity-Container').style.visibility = 'visible';
		document.getElementById('newnumberLayer').style.visibility = 'visible';
	}
}

/*function positionItOrNot() {
	if (document.getElementById('opacity-Container').style.visibility == 'visible') {
		positionIt();
	}
}*/



function hideNewnumberLayer() {
	document.getElementById('opacity-Container').style.visibility = 'hidden';
	document.getElementById('newnumberLayer').style.visibility = 'hidden';
}

//Global function for display block/none of block elements (divs)
function showHideElements(elementName) {
	if (document.getElementById(elementName).style.display == 'none') {
		document.getElementById(elementName).style.display = 'block';
	}
	else {
		document.getElementById(elementName).style.display = 'none';
	}
}

//Function for loading teknikjobb.se as source in iframe
function loadTeknikjobb() {
	document.getElementById('teknikjobbframe').src = 'http://www.teknikjobb.se/www.teknikjobb.se/Teknikens_varld/';
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(positionIt);
addLoadEvent(RunPictureCollInits);
//addLoadEvent(loadTeknikjobb);
