var m_VapourAddress = '';
$(document).ready(function(){
	carousel();
	slideshow();
	hpcarousel();
	//hpcarouseltimer();
	tabs();
	tables();
	faqs();
	furtherinfo();
	handlepdf();
	handlenewwin();
	login();
});
function handlepdf(){

	$("a[href*='.pdf']").attr({title: "(opens in a new window)"});
	$("a[href*='.pdf']").addClass("pdf");
	$("a[href*='.pdf']").click(function () { 
		createPopUp(this, "popper", "resizable,scrollbars");return false;
	});
}function handlenewwin(){

	$("a.newin").attr({title: "(opens in a new window)"});
	$("a.newin").click(function () { 
		createPopUp(this, "popper", "resizable,scrollbars");return false;
	});
}
function createPopUp(link, POPname, POPstyle){
	var strProps="";
   	window.open(link.href,POPname,POPstyle);
}

function furtherinfo(){
	$("body").append('<div id="overlay"></div>');

	$(".furtherinfo").hide();
	$(".furtherinfolink").addClass("dummylink");
	$(".furtherinfo").prepend('<p class="closer"><a href="#">Close</a></p>');
	$(".furtherinfo .closer a").bind("click", function(){
		$(this).parents("div:first").hide();
		$("#overlay").hide();
		return false;
	});
	$(".furtherinfolink").bind("click", function(){
		$(".furtherinfo").hide();
		$("#overlay").show();
		if($(this).hasClass("furtherinfolinkli")){
			
			$(this).parent().find(".furtherinfo").show();
		}else{
			$(this).parent().next().find(".furtherinfo").show();
		}
	});
}

function hpcarouseltimer(){
	if($("#home").length > 0){
		$("body").everyTime(5000, "hide", function() {
			movetonext();
		});
	}
}

function faqs(){
		if($(".faqcontent").length > 0){
			$(".faqcontent .answer").hide();
			$(".faqcontent h4").addClass("dummylink");
			$(".faqcontent h4").mouseover(function() {
				$(this).attr("style","text-decoration:underline;cursor:pointer")
			}).mouseout(function(){
				$(this).attr("style","text-decoration:none;cursor:auto;")
			}).click(function(){
				$(this).next().slideToggle();
			})
		}
}

function newcss(){
	$("head").append('<style type="text/css">@import "Content/paul.css";</style>');
}

function movetonext(){
	indeximg = $('#markers img').index($("#markers .car-selected"));
	indeximg = indeximg + 1;
	numberofitems = $("li",carouselelement).length;
	if((numberofitems - 2) > indeximg){
		$("#hp-carousel li.citem").hide();
		$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
		$("#hp-carousel #markers img").removeClass("car-selected");
		
		$("#hp-carousel #markers img:eq("+indeximg+")").attr('src','Content/themes/base/images/hp-car-selected.png');
		$("#hp-carousel #markers img:eq("+indeximg+")").addClass("car-selected");
		$("#hp-carousel li.citem:eq("+indeximg+")").fadeIn();
	}else{
		$("#hp-carousel li.citem").hide();
		$("#hp-carousel #markers img").removeClass("car-selected");
		$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
		$("#hp-carousel #markers img:eq(0)").attr('src','Content/themes/base/images/hp-car-selected.png');
		$("#hp-carousel #markers img:eq(0)").addClass("car-selected");
		$("#hp-carousel li.citem:eq(0)").fadeIn();
	}
}

function hpcarousel(){

	carouselelement = $("#hp-carousel");
	numberofitems = $("li",carouselelement).length;
	strHTML = '<p id="markers">';
	$("li",carouselelement).each(function(i){
		if(i==0){
			strHTML += '<img class="car-selected" src="Content/themes/base/images/hp-car-selected.png" alt=""/>';
		}else{
			strHTML += '<img src="Content/themes/base/images/hp-car-n-selected.png" alt=""/>';
		}
	});
	strHTML += "</p>";
	$("ul",carouselelement).prepend('<li class="movecar prev"><a href="#">Previous</a></li>');
	$("ul",carouselelement).append('<li class="movecar next"><a href="#">Next</a></li>');
	$(carouselelement).append(strHTML);
	
	$("#hp-carousel .movecar").bind("click",function(){
		$("body").stopTime();
		$("body").everyTime(5000, "hide", function() {
			movetonext();
		});
		numberofitems = $("li",carouselelement).length;
		if($(this).hasClass("prev")){
			indeximg = $('#markers img').index($("#markers .car-selected"));
			indeximg = indeximg - 1;
			numberofitems = $("li",carouselelement).length;
			if(indeximg != -1){
				$("#hp-carousel li.citem").hide();
				$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
				$("#hp-carousel #markers img").removeClass("car-selected");
				
				$("#hp-carousel #markers img:eq("+indeximg+")").attr('src','Content/themes/base/images/hp-car-selected.png');
				$("#hp-carousel #markers img:eq("+indeximg+")").addClass("car-selected");
				$("#hp-carousel li.citem:eq("+indeximg+")").fadeIn();
			}else{
				numberofitems = numberofitems - 3;
				$("#hp-carousel li.citem").hide();
				$("#hp-carousel #markers img").removeClass("car-selected");
				$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
				$("#hp-carousel #markers img:eq("+numberofitems+")").attr('src','Content/themes/base/images/hp-car-selected.png');
				$("#hp-carousel #markers img:eq("+numberofitems+")").addClass("car-selected");
				$("#hp-carousel li.citem:eq("+numberofitems+")").fadeIn();
			}
			return false;
		}else{
			indeximg = $('#markers img').index($("#markers .car-selected"));
			indeximg = indeximg + 1;
			numberofitems = $("li",carouselelement).length;
			if((numberofitems - 2) > indeximg){
				$("#hp-carousel li.citem").hide();
				$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
				$("#hp-carousel #markers img").removeClass("car-selected");
				
				$("#hp-carousel #markers img:eq("+indeximg+")").attr('src','Content/themes/base/images/hp-car-selected.png');
				$("#hp-carousel #markers img:eq("+indeximg+")").addClass("car-selected");
				$("#hp-carousel li.citem:eq("+indeximg+")").fadeIn();
			}else{
				$("#hp-carousel li.citem").hide();
				$("#hp-carousel #markers img").removeClass("car-selected");
				$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
				$("#hp-carousel #markers img:eq(0)").attr('src','Content/themes/base/images/hp-car-selected.png');
				$("#hp-carousel #markers img:eq(0)").addClass("car-selected");
				$("#hp-carousel li.citem:eq(0)").fadeIn();
			}
			return false;
		}
	});
	$("#hp-carousel #markers img").mouseover(function() {
		if(!$(this).hasClass("car-selected")){
			$(this).attr("style","cursor:pointer");
		}
	}).mouseout(function(){
		$(this).attr("style","cursor:default");
	});

	$("#hp-carousel #markers img").bind("click",function(){
		$("body").stopTime();
		$("body").everyTime(5000, "hide", function() {
			movetonext();
		});
		indeximg = $('#hp-carousel #markers img').index(this);
		$("#hp-carousel li.citem").hide();
		$('#hp-carousel #markers img').attr('src','Content/themes/base/images/hp-car-n-selected.png');
		$(this).attr('src','Content/themes/base/images/hp-car-selected.png');
		$(this).parents("p:first").find("img").removeClass("car-selected");
		$(this).addClass("car-selected");
		$("#hp-carousel li.citem:eq("+indeximg+")").fadeIn();
	});
	
	
}

function tables(){
	$("table").each(function(i){
		$("tr:even",this).addClass("alternate");
	});
}
function carousel(){
	if($("#carousel").length > 0){
		getCarousel = $("#carousel");
		$(">div:gt(1)" , getCarousel).hide();
		$("#carouselcontrols li").bind("click",function(){
			
			indexli = $('#carouselcontrols li').index(this);
			indexli = indexli + 1
			$(">div:gt(0)",getCarousel).hide()
			$(">div:eq("+indexli+")",getCarousel).fadeIn();
			$("li",getCarousel).removeClass("active");
			$(this).addClass("active");
			return false;
		});
	}
	if($("#carousel-svrspage").length > 0){
		getCarousel = $("#carousel-svrspage");
		$(">div:gt(1)" , getCarousel).hide();
		$("#carouselcontrols li").bind("click",function(){
			
			indexli = $('#carouselcontrols li').index(this);
			indexli = indexli + 1
			$(">div:gt(0)",getCarousel).hide()
			$(">div:eq("+indexli+")",getCarousel).fadeIn();
			$("li",getCarousel).removeClass("active");
			$(this).addClass("active");
			return false;
		});
	}
}
function tabs(){
	if($("#tabs").length > 0){
		
		gettabs = $("#tabs");
				
		if($(gettabs).hasClass("plain")){
			// there is a inner DIV that needs adding
			selection = ">div>div";
		}else{
			selection = ">div";
		}
		if($("#tabs #intro").length > 0){
			$(">div:gt(1)" , gettabs).hide();
		}else{
			$(selection+":gt(0)" , gettabs).hide();
		}
		strHTML = '<ul id="tabscontrols">';
		
		if($("#tabs h2").length > 0){
			strheading = "h2";
		}else if($("#tabs h3").length > 0){
			strheading = "h3";
		}else if($("#tabs h4").length > 0){
			strheading = "h4";
		}
		
		$(selection,gettabs).each(function(i){
			if($(this).attr("id") != "intro"){
				classname = $(strheading,this).text().toLowerCase()
				classname = classname.replace(" ","-");
				strClass = ' class="'+classname +'"';
				strHTML += '<li'+strClass+'><span></span><a href="#"><span>' + $(strheading,this).text() + "</span></a></li>";
			}
		});
		
		strHTML += "</ul>";
		
		$("#tabs").prepend(strHTML);
		$("#tabscontrols li:eq(0)").addClass("active").addClass("first");
		
		$("#tabscontrols li").bind("click",function(){
		
			
			gettabs = $("#tabs");
			
			if($(gettabs).hasClass("plain")){
				// there is a inner DIV that needs adding
				
				selection = ">div>div";
			}else{
				selection = ">div";
				settext = $(this).text().toLowerCase();
				$("#productfeatures li").each(function(){
					
					if($(this).hasClass(settext)){
						if($(this).is(":visible")){
							
						}else{
							$(this).show();
						}
					}else{
						$(this).hide();
					}
				})
			}
			
			if($("#tabs #intro").length > 0){
				indexli = $('#tabscontrols li').index(this);
				$(">div:gt(0)",gettabs).hide()
				indexli ++;
			}else{
				indexli = $('#tabscontrols li').index(this);
				$(selection,gettabs).hide()
			}

			$(selection+":eq("+indexli+")",gettabs).fadeIn();
			$(">ul>li",gettabs).removeClass("active");
			$(this).addClass("active");
			$("#tabs").attr("style","background:url(Content/themes/base/images/"+$(this).text()+"-banner.jpg) no-repeat 0 0")
			return false;
		});
		
		
	}
}
function slideshow(){
	if($("#slideshow").length > 0){
		getSlideshow = $("#slideshow");
		$(">div:gt(0)" , getSlideshow).hide();
		
		strHTML = '';
		counter = 0;
		
		if($("#slideshow h2").length > 0){
			strheading = "h2";
		}else if($("#slideshow h3").length > 0){
			strheading = "h3";
		}else if($("#slideshow h4").length > 0){
			strheading = "h4";
		}
		
		$(">div",getSlideshow).each(function(i){
			if(i==0){
				headingname = $(strheading,this).text();
				headingname = headingname.replace(" ","-");
				
				strClass = ' class="active"';
				straClass = ' class="'+headingname.toLowerCase()+'"';
			}else{
				headingname = $(strheading,this).text();
				headingname = headingname.replace(" ","-");
				strClass = '';
				straClass = ' class="'+headingname.toLowerCase()+'"';
			}
			strHTML += '<li'+strClass+'><a'+straClass+' href="#">' + $(strheading,this).text() + "</a></li>";
			counter = i;
		});
		counter = ((counter + 1) * 8) +.4;
		strHTML = "<span id='slideshowcontrols'><ul style='width:"+counter+"em'>"+strHTML+"</ul></span>";
		
		$("#slideshow").prepend(strHTML);
		
		$("#slideshowcontrols li").bind("click",function(){
		
			indexli = $('#slideshowcontrols li').index(this);
			$(">div",getSlideshow).hide()
			$(">div:eq("+indexli+")",getSlideshow).fadeIn();
			$("li",getSlideshow).removeClass("active");
			
			$(this).addClass("active");
			
			return false;
		});		
	}
}

///////////////////////////   Dynamic Site Section //////////////////////////////////////////////////
function login() {
    $("#account a").bind("click", function () {
        if (window.navigator.appName == "Microsoft Internet Explorer") {
            var m_VapourAddress = getCookie('m_VapourAddress');
            $("#loginpane").fadeToggle();
        }
        else {
            alert("Your browser is currently not supported by this application (" + window.navigator.appName + ")!");
        }
        return false;
    });
    strHTML = '<div id="loginpane">';
    strHTML += '<form method="POST" action="' + getCookie('m_VapourAddress') + "vapour.dll?CMDLogon" + '">';
    strHTML += '<ul>';
    strHTML += '<li>';
    strHTML += '<label for="username">User Name</label>';
    strHTML += '<input id="username" type="text" name="username">';
    strHTML += '</li>';
    strHTML += '<li>';
    strHTML += '<label for="password">Password</label>';
    strHTML += '<input id="password" type="password" name="password">';
    strHTML += '</li>';
    strHTML += '</ul>';
    strHTML += '<input type="hidden" name="style" id="style" value="1" />';
    strHTML += '<input type="hidden" name="language" id="language" value="0" />';
    strHTML += '<p class="button"><input type="submit" value="Log in" />';
    strHTML += '</form>';
    strHTML += '<div class="bottom"></div>'
    strHTML += '</div>';
    $("#all").prepend(strHTML);

}

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
};

function getCookie(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 deleteCookie(name) {
    setCookie(name, "", -1);
};

