function popupsJ() {
	
	$("a[@href$='.pdf'],a.map,a.friend").each(function(intindex){
		$(this).attr({title: $(this).attr("title") + " (opens in a new window)"});
		//$("a[@href$='pdf'],a.map,a.friend").attr({title: this + "(opens in a new window)"}); to delete
		$(this).append(" <img alt='(opens in a new window)' src='/i/new-win-icon.gif'>");
		//$("a[@href$='pdf'],a.map,a.friend").append(" <img alt='(opens in a new window)' src='/i/new-win-icon.gif'>"); to delete
	});
	
	$("a[@href$='.pdf']").click(function () { 
		createPopUp(this, "popper", "resizable,scrollbars");return false;
	});
	$("a.map").click(function () { 
		createPopUp(this, "popper", "height=450,width=375,resizable");return false;
	});
	$("a.virtual").click(function () { 
		// not sure we use this one now.
		createPopUp(this, "popper", "width=390,height=560,resizable,scrollbars");return false;
	});
	$("a.friend").click(function () { 
		createPopUp(this, "popper", "width=410,height=540,resizable,scrollbars");return false;
	});
	$("a.tc").click(function () { 
		createPopUp(this, "popper", "width=410,height=540,resizable,scrollbars");return false;
	});
}

function createPopUp(link, POPname, POPstyle){
	var strProps="";
   	window.open(link.href,POPname,POPstyle);
}

function changeMailJ(){
	$("span.email").each(function( intIndex ){
			$(this).html("<a href='mailto:" + $("span",this).text() + "@webcredible.co.uk'>" + $("span",this).text() + "@webcredible.co.uk</a>");
		}
	);
}

function cancelLinkJ() {
	$("a[href='#']").click(function () {return false;});
}

function linkIncreaseJ() {
	$("#home #ad").hover(function () {
      		$("div", this).css({ background:"#c4130d", cursor:"pointer" });
      		$("a", this).css({ color:"#fff"});
      		$("p", this).css({ color:"#fff"});
    	}, function () {
      		$("div", this).css({ background:"#f3eaef", cursor:"arrow" });
      		$("a", this).css({ color:"#c4130d"});
      		$("p", this).css({ color:"#000"});
    	});
	$("#home #ad").click(function () {
		window.location = "http://www.webcredible.co.uk" + $("a", this).attr("href");
	});
}

function prepareFormsJ() {
	newsletterEmailJ();
	$("form").each(function( intIndex ){
			$(this).submit(function () {
				return validateFormJ(this);
			});
		}
	);
}

function newsletterEmailJ() {
	$("#news .reqmail").blur(function () {
	      if (this.value == "") {
		this.value = "enter e-mail here";
	      }
	});
	$(".reqmail", "#news").focus(function () {	
	    if (this.value == "enter e-mail here") {
	      this.value = "";
	     }
	});
}


function validateFormJ(whichform) {
	if($(".requ[value='']", whichform).length > 0){ // skipped field.value == field.defaultValue (find out if useful?)
       		alert("Please complete all required fields");
        	return false;
	}
	if($(".reqmail[value='']", whichform).length > 0){
       		alert("Please enter your e-mail");
        	return false;
	}
	var email = true
	$(".reqmail, .email",whichform).each(function( intIndex ){		
			if(!checkEmail(this.value)){
			email = false
			}
		}
	);
	if(email == false){
   		alert("Please enter your e-mail again - it doesn\'t appear to be correct \n(If the problem persists please e-mail us directly at info@webcredible.co.uk)");
		return false;
	}
	var friendEmail = true
	$(".reqfrmail",whichform).each(function( intIndex ){		
			if(!checkEmail(this.value)){
				friendEmail = false;
			}
		}
	);
	if(friendEmail == false){
        	alert("Please enter your friend\'s e-mail again - it doesn\'t appear to be valid \n(if the problem persists please e-mail us directly at info@webcredible.co.uk)");
		return false;
	}
	return true;
}

function checkEmail(addr){
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,9}(?:\.[a-z]{2})?)$/i
    return filter.test(addr);
}

function maximiseSplit(){
	maximiseSplitApplied(".split div div");
	$(".split.left div:last-child").unbind();
	$(".split.right div:first-child").unbind();
	$(".split.none div").unbind();
}

function maximiseSplitApplied(strSelector){
		if($(".split div div h2 a").length > 0 || $(".split div div h3 a").length > 0) {
			$(strSelector).hover(function () {
				if(!$(this).hasClass("nohover")){
					$(this).addClass("splitHover");
				}

			}, function () {
				if(!$(this).hasClass("nohover")){
					$(this).removeClass("splitHover");
				}
			});
			$(strSelector).bind('click', function() {
				if(!$(this).hasClass("nohover")){
					if($("h2 a",this).length > 0){
						window.location = $("h2 a", this).attr("href");
					}else{
						window.location = $("h3 a", this).attr("href");
					}
				}
			});
		}

}


function maxWebcreds(){
	$("#wcs").hover(function () {
		$(this).addClass("cartoonHover")
	}, function () {
		$(this).removeClass("cartoonHover");

	});
	$("#wcs").click(function () {
		window.location = $("a", this).attr("href");
	});
}

function imageStyle(){

	if($("#home").length != 1)
	{
		if($(".portfolio").length == 0){
			$("#content img").each(function(intindex){
				if($(this).parents(".author").length == 0){

					if($(this).parents("#gallerymin").length == 0){
					
						if(this.width > 60){
						
							 if($.browser.msie){
							
								if(jQuery.browser.version > 6){
									$(this).wrap("<span class='imagestyle' style='max-width:" + (this.width + 12) +"px'><span></span></span>");
									//$(this).attr("style","width:100%")
								}
							 }else{
								$(this).wrap("<span class='imagestyle' style='max-width:" + (this.width + 12) +"px'><span></span></span>");
								$(this).attr("style","width:100%")					 
							 }


						}
					}
				}
			});

			$("#clients div").each(function(intindex){
					$(this).wrap("<div class='imagestyle' style='width:151px'><div></div></div>");
			});
		}
	}
}

function newslettertabs(){
	if($("#newstabs").length > 0){
		feedscontent = '<div class="feeds tab2" id="tab2">' + $("#newstabs #feeds").html() + '</div>';
		twittercontent = '<div id="tab3" class="twitter tab3">' + $("#newstabs #twitter").html() + '</div>';
		$("#newstabs").remove();
		$("#news").wrapInner('<div id="tab1" class="newsletter active tab1"/>')
		$("#news").append(feedscontent);
		$("#news").append(twittercontent);
		$("#news").addClass("js");

		$("#news div.newsletter ~ div").hide();


		$("#news").before('<div id="tabbntf" class="ready"><ul><li class="newslettertab"><a class="1 selectedtab" href="#"><span class="off">Newsletter</span></a></li><li class="feedstab"><a class="2" href="#"><span class="off">Feeds</span></a></li><li class="twittertab"><a class="3" href="#"><span class="off">Tweets</span></a></li></ul></div>');
		
		// added
		$.ajax({
			url: 'http://www.webcredible.co.uk/incs/tweets.php',
			success: function(data) {
				$('#tab3 li').remove();
				$('#tab3 p:first').text('Our latest Tweets include:');
				$('#tab3 ul').append(data);
				}
			});

		//added
		$("#tabbntf ul a").click(function () {
	
			itemnumber = $(this).attr("class").split(" ");
			if(itemnumber.length > 0){
				itemnumber = itemnumber[0]
			}else if (itemnumber.length == 0){
				itemnumber = itemnumber[0]

			}

			if(itemnumber > 0 && itemnumber < 4){
				if(!$(".tab"+itemnumber).hasClass("active")){
					if($("#tabbntf").hasClass("ready")){
						
						$("#tabbntf").removeClass("ready");
						if($.browser.msie){
							if(jQuery.browser.version > 6){
								$("#news div.active").slideUp(300, function() {	
									$(".tab"+itemnumber).slideDown(300, function() {
										$("#news div").removeClass("active");
										$("#tabbntf a").removeClass("selectedtab");
										settab = $(this).attr("id");
										settab = $(this).attr("class").split("tab");
										$("#tabbntf a." + settab[1]).addClass("selectedtab");
										$("#tabbntf a." + settab[1]).focus();
										$("#news #tab" + settab[1]).addClass("active");
										$("#tabbntf").addClass("ready");
									});
								});
							}else{
								
								$("#news div.active").hide();
								$("#news div").removeClass("active");
								settab = $(this).attr("class");
								$("#tabbntf a").removeClass("selectedtab");
								$("#tabbntf a." + settab).addClass("selectedtab");
								$("#news #tab" + settab).show();
								$("#news #tab" + settab).addClass("active");	
								$("#tabbntf").addClass("ready");
							}
						}else{
								$("#news div.active").slideUp(300, function() {	
									$(".tab"+itemnumber).slideDown(300, function() {
										$("#news div").removeClass("active");
										$("#tabbntf a").removeClass("selectedtab");
										settab = $(this).attr("id");
										settab = $(this).attr("class").split("tab");
										$("#tabbntf a." + settab[1]).addClass("selectedtab");
										$("#tabbntf a." + settab[1]).focus();
										$("#news #tab" + settab[1]).addClass("active");
										$("#tabbntf").addClass("ready");
										
									});
								});
						}
					}
				}	
			}			
			return false;
		});
	}
}

function smallgallery(){
	
	if($("#gallerymin span.imagetitle").length > 0){
	
		var blnshowtitle = false;
		
		if ($("#gallerymin").hasClass("showlabels")){
			blnshowtitle = true;
		}
	
		var widthgallery = (parseInt($("#gallerymin span.imagetitle").length) * 3.5) + "em;";
	
		var relativeli =  (parseInt($("#gallerymin span.imagetitle").length) * 3) / 2 + "em;";

		var newgallery = '<ul class="galleryminpag" style="width:'+widthgallery+'">';
		
		$("#gallerymin ul.galleryimages li").each(function( intIndex ){
			intIndex++;	
			$(this).addClass("image" + intIndex);
			if(!blnshowtitle){
				$(this).find("img").attr("alt",$(this).find(".imagetitle").text());
			}
		});
		
		$("#gallerymin span.imagetitle").each(function( intIndex ){
			intIndex++;
			if(intIndex == 1){
				addSelected = ' class ="selected"'
			}else{
				addSelected = ""
			}
			newgallery += '<li' + addSelected + ' style="left:-' + relativeli + '"><a class="gallerymin' + intIndex + '" href="#"><span>' + intIndex + "</span> <span class='hidden'>" +  $(this).text()  + '</span></a></li>';
			$(this).parents("li:first").attr("style", "z-index:"+ (100 - intIndex))

			if(!blnshowtitle){
				$(this).remove();
			}
		});
		newgallery += "</ul>";
		$("#gallerymin").append(newgallery)
		
		$(".galleryminpag a").click(function () {
			if(!$(this).parents("li:first").hasClass("selected")){
				var classis = $(this).attr("class");
				classis = classis.replace("gallerymin", "");

				$("#gallerymin ul.galleryimages li").fadeOut();
				$("#gallerymin ul.galleryimages li:nth-child(" + classis + ")").fadeIn('slow', function() {
					$(".galleryminpag li").removeClass("selected");
					classis = classis.replace("image", "");
					$("#gallerymin li:nth-child(" + classis + ")").addClass("selected");
				});
			}
			return false;
		});
	}
}

function footnoteLinks2thesequel(){
		printlinks = new Array();
		counter = 0;
		
		idToFind = "";
		
		if($("#blogcontainer").length == 1){
			idToFind = "#blogcontainer";
		}else{
			idToFind = "#content";
		}
		$(idToFind + " *").each(function(index) {	
			
			if(this.nodeName.toLowerCase() == "a"){
				
				collectParents = $(this).parents();
				blnAdd = true;
				$(collectParents).each(function(index) {
					
					if($(this).hasClass("nop")){
						blnAdd = false;
					}else if($(this).attr("id") == "nop"){
						blnAdd = false;
					}
				});
				if(blnAdd){
					printlinks[counter] = $(this);
					$(this).after('<sup class="printOnly">'+(counter + 1)+'</sup>');
					counter++;
				}
				
			}
		});
		
		strPrintLinks = "<h2 class=\"printOnly\">Links</h2>";
		
		if(printlinks.length > 0){
			strPrintLinks += "<ol class=\"printOnly\" style='clear:both'>";
			$(printlinks).each(function(index) {		
				strPrintLinks += "<li>" + $(this).attr("href") + "</li>";
			});
			strPrintLinks += "</ol>";
		}
		
		$(idToFind).append(strPrintLinks)
		
}

$(document).ready(function(){

	javaScriptEnabled();
	footnoteLinks2thesequel()
	imageStyle();
	smallgallery();
	popupsJ();
	cancelLinkJ();
	linkIncreaseJ();
	prepareFormsJ();
	maximiseSplit();
	maximiseTraining();
	maxWebcreds();
	createViewingOptionsHTML();
	showMore();
	topicsOrTags();
	moreLinks();
	showHideDiscounts();
	calenderRoundedCorners();
	homecurves();
	bannercurves();
	
	//homepageamiation();

	if($.browser.msie){
		if(jQuery.browser.version >= 6){
			newslettertabs();
		}else{

		}
	}else{
		newslettertabs();
	}
});

function homepageamiation(){
	counter=1;
	$("#banNav a").each(function(){
		$(this).addClass("hpnav-" + counter);
		counter++;
	});
	
	$("#htagline div.other h2").attr("style", "display:none");
	$("#htagline div.other p").attr("style", "display:none");
	$("#banNav a").bind("mouseenter", function(){
		
		$("#htagline>div").removeClass("othervisible");
		$("#htagline>div.first h1").fadeOut();
		$("#htagline>div.first p").fadeOut();

		classArray = $(this).attr("class").split("-");
		$("#htagline>div:eq("+classArray[1]+")").addClass("othervisible");
		$("#htagline>div:eq("+classArray[1]+") h2").fadeIn();
		$("#htagline>div:eq("+classArray[1]+") p").fadeIn();
	}).bind("mouseleave", function(){
		$("#htagline>div").removeClass("othervisible")
		$("#htagline>div.first h1").fadeIn();
		$("#htagline>div.first p").fadeIn();
	});
	
}

function bannercurves(){

	if($("#pbanners").length == 1){

		$("#pbanners div").prepend('<div class="btl"></div><div class="btr"></div>');
	}
}
function homecurves(){
	$("#banNav").wrapInner("<div><div><div><div><div></div></div></div></div></div>")
}
function maximiseTraining(){

	$(".streamdays li").hover(function () {
      			$(this).css({ border:"1px solid #adcede",backgroundColor:"#e8f9fc", cursor:"pointer" });
      			$("a",this).addClass("psuedohover")
    	}, function () {
      		$(this).css({ border:"1px solid #fff",backgroundColor:"#fff", cursor:"arrow" });
      		$("a",this).removeClass("psuedohover")
      		
    	});
    	
    	$(".streamdays li").click(function () {
    		window.location = $("a", this).attr("href");
    	});
}

function showHideDiscounts(){
	// Hide content by default
	$(".slink a").parent().next().addClass("off");
	
	$(".slink a").click(function() {
		// Show content when link is cliked on
		$(this).parent().next().toggleClass("off");
	});
}

function calenderRoundedCorners(){
	jQuery("span.d-b").wrapInner("<span class='d-bl'><span class='d-br'><span class='d-tl'><span class='d-tr'></span></span></span></span>");
	jQuery("span").removeClass('d-b');
	
	jQuery("strong.cm").wrap("<span class='d-m-tl'><span  class='d-m-tr'></span></span>");
}



function moreLinks(){
	$(".showMore a").click(function () {
		if($(this).text() == "More tags"){
			$(this).text("Hide tags");
		}else if($(this).text() == "Hide tags"){
			$(this).text("More tags");
		}
	});
}
function javaScriptEnabled(){
	$("body").addClass("javascript");
}

function topicsOrTags(){
	$(".subTabs .tag ul").hide();
	
	$(".subTabs .tag").addClass("jtag");
	$(".subTabs .tag").removeClass("tag");
	
	$(".subTabs .topic").addClass("jtopic");
	$(".subTabs .topic").removeClass("topic");
	
	//$(".subTabs .jtopic").attr("style","height:9.7em");
	
	$(".subTabs .jtopic").click(function () {
	
		//$(".subTabs .jtag").attr("style","height:");
		$(".subTabs .jtag ul").hide();
		$(".subTabs .jtopic ul").show();
	});
	$(".subTabs .jtag").click(function () {
		$(".subTabs .jtopic ul").hide();
		//$(".subTabs .jtag").attr("style","height:82.2em");
		$(".subTabs .jtag ul").show();
	});
}

function showMore(){
	$(".showMore").each(function () {
		var Classes = $(this).attr("class").split(" ");
		
		for(var x=0; x < Classes.length; x++){
			targetNumber = Classes[x].split("target");
			if(targetNumber.length > 1){				
				$(this).wrapInner("<a href='#'></a>");
				$("#" + Classes[x]).hide();
				$(this).click(function () {
					var ClassesForToggle = $(this).attr("class").split(" ");
					for(var y=0; y < ClassesForToggle.length; y++){
						targetNumberForLink = Classes[y].split("target");
						if(targetNumberForLink.length > 1){
							$("#" + Classes[y]).slideToggle("fast");
							return false
						}
					}
				});
			}
			else{
			}
		}
   	});

}

function extractCSSValue(){
	var cookies, cookie, tmparr, name, value, arrlen;
	cookies = document.cookie.split(';');
	arrlen = cookies.length;
	for(var i=0; i<arrlen; i++){
		tmparr = cookies[i].split('=');
		name = tmparr[0];
		value = tmparr[1];
		if(name.trim() == 'css' && value.substring(0,1) != 0){
			return "/non-default_font_size";
		}
	}
	return null;
}

String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g,"");
}

function createViewingOptionsHTML() {
	
	var strColourHeading = "<h2 class='off'>Colour scheme options</h2>";

	var ColorDescriptions = new Array(); // array that stores the text descriptions of the viewing options	
	
	ColorDescriptions[0]="Regular display";
	ColorDescriptions[1]="High contrast";
	ColorDescriptions[2]="Dark blue on cream";
	ColorDescriptions[3]="Dark blue on light blue";
		
	
	$(".moreviewingoptions").hide();
	$(".moreviewingoptions div").append(strColourHeading);
	$(".moreviewingoptions div").append("<ul id='colourscheme' class='colouroff'>");
	
	for (var x = 0; x <= (ColorDescriptions.length - 1); x++) {
	   $(".moreviewingoptions #colourscheme").append("<li class='lay" + (x + 1) + "'><span class='displaytext'>Sample text</span> <a href='/incs/selectcss.php?colour=" + x + "'>" + ColorDescriptions[x] + "</a></li>")
	}
	
	$(".options").click(function () {
		$(".moreviewingoptions").slideToggle("fast", function (){
		 	if($(".moreview").html() == "More viewing options"){
		 		$(".moreview").html("Hide viewing options");
		 	}else{
		 		$(".moreview").html("More viewing options");
		 	}
			$(".moreview").toggleClass("lessview");
		});
   		return false;
   	});
   	
	$("#fonts a").click(function () {
		$("#fonts a").removeClass("selected")
		$(this).toggleClass('selected');
   	});
	$("#colourscheme a").click(function () {
		$("#colourscheme a").removeClass("selected")
		$(this).toggleClass('selected');
   	});
}

