// JavaScript Document

///////////////////////////////////////////////////////////////////////////////////////// 
// Open offsite links in new window
/////////////////////////////////////////////////////////////////////////////////////////

//Open offsite links in new window script- http://www.dynamicdrive.com/
//Created: August 28th, 2007'

var ddwindowlinks={
//1)Enter domains to be EXCLUDED from opening in new window:
excludedomains: ["new.swiftwatercellars.com", "swiftwatercellars.lytleworks.com", "www.swiftwatercellars.com", "swiftwatercellars.com", "test.swiftwatercellars.com", "76.191.117.78", "216.243.10.39"],

//2) Target for links that should open in a new window (ie: "_blank", "secwin" etc):
linktarget: "_blank",

//3) Specify operating mode ("auto" or "manual"):
mode: "auto",

//4) If mode is "manual", customize checkbox HTML to show to users (Preserve id attribute):
toggleHTML: '<form><input type="checkbox" id="targetcheckbox" checked="checked" /><label for="targetcheckbox">Open off-site links in new window?</label></form>',

//5) If mode is "manual", enable user persistence so the state of the checkbox is remembered?
persist: true,

assigntarget:function(){
	var rexcludedomains=new RegExp(this.excludedomains.join("|"), "i")
	var all_links=document.getElementsByTagName("a")
	if (this.mode=="auto" || (this.mode=="manual" && this.togglebox.checked)){
		for (var i=0; i<=(all_links.length-1); i++){
			if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1)
				all_links[i].target=ddwindowlinks.linktarget
		}
	}
	else{
		for (var i=0; i<=(all_links.length-1); i++)
			all_links[i].target=""
	}
	if (this.mode=="manual" && this.persist)
		this.setCookie("dlinktarget", (this.togglebox.checked)? "yes" : "no", 30) //remember user setting for 30 days (set to -1 then reload page to erase cookie)
},

init:function(){
	if (document.getElementById && this.mode=="manual"){
		document.write(this.toggleHTML)
		this.togglebox=document.getElementById("targetcheckbox")
		this.togglebox.onclick=function(){ddwindowlinks.assigntarget()}
		if (this.persist && this.getCookie("dlinktarget")!="")
			this.togglebox.checked=(this.getCookie("dlinktarget")=="yes")? true : false
	}
	if (window.addEventListener)
		window.addEventListener("load", function(){ddwindowlinks.assigntarget()}, false)
	else if (window.attachEvent)
		window.attachEvent("onload", function(){ddwindowlinks.assigntarget()})
},

getCookie:function(Name){
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return ""
},

setCookie:function(name, value, days){
	var expireDate = new Date()
	//set "expstring" to either an explicit date (past or future)
		var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
		document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
}

}

ddwindowlinks.init()


//////// BASIC COOKIE SCRIPTS

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function showInfo(id) {
	document.getElementById(id).setAttribute("class", "tooltip_on");

}

function hideInfo(id) {
	document.getElementById(id).setAttribute("class", "tooltip");

}

// IE BROWSER SNIFFING

function idBrowser () {

    var returnValue;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 	var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 	if (ieversion>=8)
		returnValue = "IE8+";
	else if (ieversion>=7)
  		returnValue = "IE7";
 	else if (ieversion>=6)
 		 returnValue = "IE6";
	}
	return returnValue;
}

// BASIC AJAX

function getAjax(id,action) {
	
	 // ID is the id of the page item to be replaced with the ajax result; 
	 // Action is the parameter for index to select the right switch case
	 $("#"+id).load('/index.php?id=search_ajax&action='+action, function() {
																		 
		// Put any work to be done after ajax result is loaded.
	
	 } );

}

// Accordian (news page)
//function expandIt(id) {
//		alert(id);
//		$('.opened').fadeIn('slow');
//		$('.opened').removeClass('opened');
									
//		$('#section'+id+' .collapse').slideDown('slow');
//		$('#section'+id+' .collapse').addClass('opened');
									
//		return false;								
//		} 
