function mDebug(message)
{
  if(document.getElementById && document.getElementById("debug"))
  {
    var debugged = document.getElementById("debug");
    var para = document.createElement("p");
    var mess = document.createTextNode(message);
    para.appendChild(mess);
    debugged.appendChild(para);
  }
}
function externalLinks()
{
  if(!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for(var i = links.length - 1; i > -1; i--)
  {
    if(links[i].getAttribute("href") && links[i].getAttribute("rel") == "external")
      links[i].target ="_blank";
  }
}
function popupLinks()
{
  if(!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for(var i = links.length - 1; i > -1; i--)
  {
    if(links[i].getAttribute("href"))
    {
			if(links[i].getAttribute("rel") == "popup" || links[i].getAttribute("rel") == "newwin"){
				links[i].onclick = function(){
	        newwindow = window.open(this.getAttribute("href"), 'PR','scrollbars=yes,width=810,height=580,toolbar=yes');
	        if (window.focus) { newwindow.focus() }
	        return false;
	      }
	      links[i].onkeypress = links[i].onclick;
			}
    }
  }
}
function lightboxLinks()
{
  if(!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for(var i = links.length - 1; i > -1; i--)
  {
    if(links[i].getAttribute("href"))
    {
			if(links[i].getAttribute("rel") == "iframe"){
				links[i].onclick = function(){
	        prepBox(this);
	        return false;
	      }
	      links[i].onkeypress = links[i].onclick;
			}
    }
  }
}
function setUpTabs(){
	$$('#eventnewspapers .tabs a').each(function(tab){
		tab.observe('mouseover',changeTab)
	});
}
function changeTab(event){
	Event.stop(event);
	element = event.element();
	$$('#eventnewspapers a').each(function(link){
		if(link.hasClassName('active')){
			link.toggleClassName('active');
		}
	});
	element.toggleClassName('active');
	target = element.readAttribute('href').replace('#','')
	$$('#eventnewspapers-content div').each(function(tab){
		if(!tab.hasClassName('hidden')){
			tab.toggleClassName('hidden');
		}
		if(tab.readAttribute('id') == target){
			tab.toggleClassName('hidden');
		}
	});
	
}

function slideVertical(event){
	trigger = /[0-9]+/;
	trigger = trigger.exec(event.element().readAttribute('href'))[0] - 1;
	tar = $('productslide');
	temp = tar.select('.product-listing')
	height = 0;
	for( i=0;i < temp.length && i < trigger; i++){
		height -= temp[i].getHeight() + 8;
	}
	if(height != $('productslide').getStyle('top').replace("px","")){
		temp = new Effect.Tween($('productslide'), $('productslide').getStyle('top').replace("px",""), height, { duration: 1.0, afterFinish: function() {} }, function(p){ $('productslide').setStyle({top: p + "px"}); });
	}
}

function slideHorizontal(event){
	trigger = /[0-9]+/;
	trigger = trigger.exec(event.element().readAttribute('href'))[0] - 1;
	$$('.slidetrigger').each(function(link){
		Element.extend(link.parentNode).removeClassName('active');
	});
	event.element().parentNode.addClassName('active');
	tar = $('productslide');
	temp = tar.select('.product-listing')
	width = 0;
	for( i=0;i < temp.length && i < trigger; i++){
		width -= temp[i].getWidth() + 8;
	}
	if(width != $('productslide').getStyle('left').replace("px","")){
		temp = new Effect.Tween($('productslide'), $('productslide').getStyle('left').replace("px",""), width, { duration: 1.0, afterFinish: function() {} }, function(p){ $('productslide').setStyle({left: p + "px"}); });
	}
}

function slideTriggers(){
	$$('.slidetrigger').each(function(trigger){
		trigger.observe('click',slideHorizontal)
	});
}

sfHover = function() {
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}
function setupPopup(className){
		if($('quote_form')){
			new Popup('quote_form', null, {modal:true});
			var links = $$('.' + className);
			links.each(function(link){
				link.observe("click", popThis);
			});
		}
	}
	function popThis(event){
		Event.stop(event);
		element = event.element();
		$('quote_form').popup.show();
	}
	function contactFormCheck(event){
		contactform = Event.element(event);
		errors = "";
		if(contactform['fromEmail'].length == 0){
			errors += " - email\n";
		}
		if(contactform['sender'].length == 0){
			errors += " - name]\n";
		}
		if(contactform['phone'].length == 0){
			errors += " - phone number\n";
		}
		if(errors.length > 0){
			event.stop();
			alert("Please fill in the required field(s):\n" + errors);
		}
	}
window.onload = function() {
  externalLinks();
  popupLinks();
	lightboxLinks();
	slideTriggers();
	setupPopup('quote_req');
	if($('eventnewspapers')){ setUpTabs(); }
	temp = $$("#quote_form form")
	if(temp.length > 0){
		temp = temp[0];
		temp.observe('submit', contactFormCheck);
	}
  if (window.attachEvent) { window.attachEvent("onload", sfHover); }
}
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function confirmation(question, target) {
	var answer = confirm(question)
	if (answer){
		window.location = target;
	}
	return false;
}

/*window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);*/
