//script file for www.connoisseurint.se

//scrolling functions for the ref_field in references.php

defaultStep=1; 
step=defaultStep; 

function scrollDown(id)
{
	clearTimeout(timerDown);
	document.getElementById(id).scrollTop+=step; 
	timerDown=setTimeout("scrollDown('"+id+"')",10);
} 

function scrollUp(id)
{
	clearTimeout(timerUp);
	document.getElementById(id).scrollTop-=step; 
	timerUp=setTimeout("scrollUp('"+id+"')",10);
} 

timerDown=""; 
timerUp="";

function stopMe()
{
	clearTimeout(timerDown);
	clearTimeout(timerUp);
}

//document.onmousemove=function(){stopMe()}  

//function to change the active reference on references.php
function setActiveRef(nr)
{
	var topEl = document.getElementById("ref_field");
	var count = topEl.getElementsByTagName("div").length;
	
	var i;
	var el;
	var activeEl;
	var activeFontEl;
	
	for(i=1;i<count+1;i++)
	{
		el = document.getElementById("ref_" + i);
		el.style.background = "#151A21";
		document.getElementById("ref_" + i + "_h3").style.color = "#C4C4C5";
	}
	
	activeEl = document.getElementById("ref_" + nr);
	activeEl.style.background = "#252A30";
	
	activeFontEl = document.getElementById("ref_" + nr + "_h3");
	activeFontEl.style.color = "#BD8828";
	
}

//Gets the reference
function getReference(id)
{
	/* Detta block gör ett AJAX-anrop till servern*/

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{	
			if(xmlhttp.status == 200)
			{
				
				var res;
				
				res = xmlhttp.responseText;
				
				if(res != "")
				{
					document.getElementById('main_info_container').innerHTML = res;
					$jq("#main_container").equalHeights(true);

				}
				
			}

		}

	}	
	
	xmlhttp.open("GET","../includes/getReference.php?id=" + id + "&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	
	
	
}

//Gets the reference logotype
function getReferenceLogo(id)
{
	/* Detta block gör ett AJAX-anrop till servern*/

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{	
			if(xmlhttp.status == 200)
			{
				
				var res;
				
				res = xmlhttp.responseText;
				
				if(res != "")
				{
					document.getElementById('ref_logo_field').innerHTML = res;
				}
				
			}

		}

	}	
	
	xmlhttp.open("GET","../includes/getReferenceLogo.php?id=" + id + "&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	
	
}

//Gets the reference image
function getReferencePic(id)
{
	/* Detta block gör ett AJAX-anrop till servern*/

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{	
			if(xmlhttp.status == 200)
			{
				
				var res;
				
				res = xmlhttp.responseText;
				
				if(res != "")
				{
					document.getElementById('ref_img_field').innerHTML = res;
				}
				
			}

		}

	}	
	
	xmlhttp.open("GET","../includes/getReferencePic.php?id=" + id + "&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	


}

//Sets the active link in advertise.php
function setActiveLink(nr)
{
	var topEl = document.getElementById("link_field");
	var count = topEl.getElementsByTagName("div").length;
	
	var i;
	var el;
	var activeEl;
	var activeFontEl;
	
	for(i=1;i<count+1;i++)
	{
		el = document.getElementById("ref_" + i);
		el.style.background = "#151A21";
		document.getElementById("ref_" + i + "_h3").style.color = "#C4C4C5";
	}
	
	activeEl = document.getElementById("ref_" + nr);
	activeEl.style.background = "#252A30";
	
	activeFontEl = document.getElementById("ref_" + nr + "_h3");
	activeFontEl.style.color = "#BD8828";
	
}

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$jq.fn.equalHeights = function(px) {
	$jq(this).each(function(){
		var currentTallest = 0;
		$jq(this).children().each(function(i){
			$jq(this).children().css({'min-height': 0});
			//$(this).css({'height': 0});
			//alert($(this).height());
			if ($jq(this).height() > currentTallest) { currentTallest = $jq(this).height(); }
			
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($jq.browser.msie && $jq.browser.version == 6.0) { $jq(this).children().css({'height': currentTallest}); }
		$jq(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

//Function for users to download files
function downloadFile(file,res)
{
	var elemIF = document.createElement("iframe");
	elemIF.src = "../includes/downloadFile.php?file=" + file + "&res=" + res;
	elemIF.style.display = "none";
	document.body.appendChild(elemIF); 

}

//Function for users to download files
function downloadIbFile(dir,file,res)
{
	
	var elemIF = document.createElement("iframe");
	elemIF.src = "../includes/downloadIbFile.php?dir=" + dir + "&file=" + file + "&res=" + res;
	elemIF.style.display = "none";
	document.body.appendChild(elemIF); 

}

//Function for users to download files
function downloadLOFile(loi)
{
	var elemIF = document.createElement("iframe");
	elemIF.src = "../includes/downloadLOFile.php?loi=" + loi;
	elemIF.style.display = "none";
	document.body.appendChild(elemIF); 

}

function Delete(question,id)
{
	var answer = confirm(question);

	if(!answer)
	{
		return;
	}
	else
	{
		location.href = "delete_faq_confirm.php?id="+id;
	}
	
}

function DeleteCitat(question,id)
{
	var answer = confirm(question);

	if(!answer)
	{
		return;
	}
	else
	{
		location.href = "delete_citat_confirm.php?id="+id;
	}
	
}

function DeletePDF(question,id)
{
	var answer = confirm(question);

	if(!answer)
	{
		return;
	}
	else
	{
		location.href = "delete_pdf_confirm.php?id="+id;
	}
	
}

//Skapar en floating-window för att visa före/efter bilder
var picwindow;

function LoadPicWindow(pic)
{	
	/* ladda in css dynamiskt */
	$$('head').first().insert(
		new Element(
			'link',
			{href:'/css/style.css', rel:'stylesheet', type:'text/css'}
		)
	);

	/* Ladda in javascript för Lightbox dynamiskt */
	$$('head').first().insert(
		new Element(
			'script',
			{
				src: '/js/lightbox.js',
				type:'text/javascript'
			}
		)
	);

	new Ajax.Request('/includes/showPic.php', {
		method: 'get',
		parameters: {pic: pic},
		onSuccess: function(transport)
		{	
			if($('box_wrapper') == null)
			{

				var myDiv = new Element('div', {'id': 'box_wrapper'});
				myDiv.innerHTML = transport.responseText;
				$(myDiv).hide();
				$$('body').first().insert(myDiv);

			}
			else
			{

				$('box_wrapper').innerHTML = "";
				$('box_wrapper').innerHTML = transport.responseText;
			}

			picwindow = new Lightbox('box_wrapper');

			picwindow.open();


			/*Event.observe(
				'FloatingWindowLink',
				'click',
				function()
				{
					lightbox.close();
				});*/
		}
	});
}

function LoadGEPicWindow(pic)
{	
	/* ladda in css dynamiskt */
	$$('head').first().insert(
		new Element(
			'link',
			{href:'/css/style.css', rel:'stylesheet', type:'text/css'}
		)
	);

	/* Ladda in javascript för Lightbox dynamiskt */
	$$('head').first().insert(
		new Element(
			'script',
			{
				src: '/js/lightbox.js',
				type:'text/javascript'
			}
		)
	);

	new Ajax.Request('/includes/showGEPic.php', {
		method: 'get',
		parameters: {pic: pic},
		onSuccess: function(transport)
		{	
			if($('box_wrapper') == null)
			{

				var myDiv = new Element('div', {'id': 'box_wrapper'});
				myDiv.innerHTML = transport.responseText;
				$(myDiv).hide();
				$$('body').first().insert(myDiv);

			}
			else
			{

				$('box_wrapper').innerHTML = "";
				$('box_wrapper').innerHTML = transport.responseText;
			}

			picwindow = new Lightbox('box_wrapper');

			picwindow.open();


			/*Event.observe(
				'FloatingWindowLink',
				'click',
				function()
				{
					lightbox.close();
				});*/
		}
	});
}

function LoadOurEventPicWindow(pic)
{	
	/* ladda in css dynamiskt */
	$$('head').first().insert(
		new Element(
			'link',
			{href:'/css/style.css', rel:'stylesheet', type:'text/css'}
		)
	);

	/* Ladda in javascript för Lightbox dynamiskt */
	$$('head').first().insert(
		new Element(
			'script',
			{
				src: '/js/lightbox.js',
				type:'text/javascript'
			}
		)
	);

	new Ajax.Request('./includes/showOurEventPic.php', {
		method: 'get',
		parameters: {pic: pic},
		onSuccess: function(transport)
		{	
			if($('our_event_pic_box_wrapper') == null)
			{

				var myDiv = new Element('div', {'id': 'our_event_pic_box_wrapper'});
				myDiv.innerHTML = transport.responseText;
				$(myDiv).hide();
				$$('body').first().insert(myDiv);

			}
			else
			{

				$('our_event_pic_box_wrapper').innerHTML = "";
				$('our_event_pic_box_wrapper').innerHTML = transport.responseText;
			}

			picwindow = new Lightbox('our_event_pic_box_wrapper');

			picwindow.open();


			/*Event.observe(
				'FloatingWindowLink',
				'click',
				function()
				{
					lightbox.close();
				});*/
		}
	});
}

function LoadEventPicWindow(pic)
{	
	/* ladda in css dynamiskt */
	$$('head').first().insert(
		new Element(
			'link',
			{href:'/css/style.css', rel:'stylesheet', type:'text/css'}
		)
	);

	/* Ladda in javascript för Lightbox dynamiskt */
	$$('head').first().insert(
		new Element(
			'script',
			{
				src: '/js/lightbox.js',
				type:'text/javascript'
			}
		)
	);

	new Ajax.Request('./includes/showEventPic.php', {
		method: 'get',
		parameters: {pic: pic},
		onSuccess: function(transport)
		{	
			if($('event_pic_box_wrapper') == null)
			{

				var myDiv = new Element('div', {'id': 'event_pic_box_wrapper'});
				myDiv.innerHTML = transport.responseText;
				$(myDiv).hide();
				$$('body').first().insert(myDiv);

			}
			else
			{

				$('event_pic_box_wrapper').innerHTML = "";
				$('event_pic_box_wrapper').innerHTML = transport.responseText;
			}

			picwindow = new Lightbox('event_pic_box_wrapper');

			picwindow.open();


			/*Event.observe(
				'FloatingWindowLink',
				'click',
				function()
				{
					lightbox.close();
				});*/
		}
	});
}
