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 != 1){
			$("#content img").each(function(intindex){
				if(this.width > 60){
					$(this).wrap("<span class='imagestyle' style='width:" + (this.width + 12) +"px'><span></span></span>");
				}
			});

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

$(document).ready(function(){
	javaScriptEnabled();
	createFlashObject()
	imageStyle();
	popupsJ();
	cancelLinkJ();
	linkIncreaseJ();
	prepareFormsJ();
	maximiseSplit();
	maxWebcreds();
	createViewingOptionsHTML();
	showMore();
	topicsOrTags();
	moreLinks();
	showHideDiscounts();
	calenderRoundedCorners();
});


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');
   	});
}

function createFlashObject(){

	if(jQuery.browser.msie && jQuery.browser.version > "5.5" || !jQuery.browser.msie){
		
		if(checkForFlash()){
			var numberOfServices = $("#banners h2").length - 1;
			var defaultHeading = $("#banners .first h1").text().replace("&", "and");
			var defaultParagraph = $("#banners .first p").text().replace(/\&/g, "and");
			
			var serviceheadings = "";
			$("#banners h2").each(function(intindex){
				removeAnd = $(this).text().replace("&", "and");
				serviceheadings = serviceheadings + ":" + removeAnd;
			});


			var serviceParagraphs = "";
			$("#banners div p").each(function(intindex){
				if($(this).parent().attr("class") != "first"){
				removeAnd = $(this).text().replace("&", "and");
				serviceParagraphs = serviceParagraphs + ":" + removeAnd;
				}
			});
			

			strCSSText = "standard";
			strCSSColor = "standard";

			CSSFiles = $("link");

			$("link").each(function(intindex){
				if($(this).attr("type") == "text/css" && $(this).attr("media") == "all"){
					if($(this).attr("href") == "/css/med.css" || $(this).attr("href") == "/css/lar.css"){
						strCSSTextB4 = $(this).attr("href").replace("/css/","");
						strCSSTextB4 = strCSSTextB4.replace(".css","");
						strCSSText = strCSSTextB4;

					}else{
						strCSSColorB4 = $(this).attr("href").replace("/css/","");
						strCSSColorB4 =strCSSColorB4.replace(".css","");
						strCSSColor = strCSSColorB4;

					}
				}
			});



			var strFlash = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"\" width=\"100%\" height=\"264\"  id=\"wcMovie\" align=\"middle\">"
			strFlash += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"
			strFlash += "<param name=\"movie\" value=\"services.swf\" />"
			strFlash += "<param name=\"wmode\" value=\"transparent\">"
			strFlash += "<param name=\"quality\" value=\"high\" />"
			strFlash += "<param name=\"FlashVars\" value=\"focusDetected=" + 0 + "&strintButtons=" + numberOfServices + "&strCSSText=" + strCSSText + "&strCSSColor=" + strCSSColor + "&strDefaultHeading=" + defaultHeading + "&strDefaultContent=" + defaultParagraph +"&strHeading='" + serviceheadings + "&serviceParagraphs='" + serviceParagraphs + "" + "\"/>"
			strFlash += "<embed src=\"services.swf\" FlashVars=\"focusDetected=" + 0 + "&strintButtons=" + numberOfServices + "&strCSSText=" + strCSSText + "&strCSSColor=" + strCSSColor + "&strDefaultHeading=" + defaultHeading + "&strDefaultContent=" + defaultParagraph +"&strHeading='" + serviceheadings + "&serviceParagraphs='" + serviceParagraphs + "" + "\" wmode=\"transparent\" quality=\"high\" width=\"100%\" height=\"290\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" swLiveConnect=\"true\" name=\"wcMovie\"/>"
			strFlash += "</object>";
			
			
			$("#banners div div div[class!='flashcontainer']").addClass("off");
			$("#banNav ul").addClass("off");
			
			$(".flashcontainer").attr("style","height:250px");
			$("#banners").attr("style","margin-bottom:0px");
			$(".flashcontainer").show();
			$("#banners").addClass("flashbanner");
			$(".flashcontainer div").html(strFlash);
			

			 $("#banNav a").focus(function (){
					navNum = $(this).attr("class")
					focusState(navNum);
			 });
			 
			 $(".tabclear").focus(function (){
			 	focusState(-1);
			 	
			 });
		 }
	}
}

function focusState(intButton){
	if(window.wcMovie){
		window.document["wcMovie"].SetVariable("focusDetected", intButton);
	}
	if(document.wcMovie){
		document.wcMovie.SetVariable("focusDetected", intButton)
	}
}

function checkForFlash(){
	var isInstalled = false;
	var version = null;
	if (window.ActiveXObject) {
		
	    var control = null;
	    try {
		control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
	    } catch (e) {

	    }
	    if (control) {
		isInstalled = true;
		version = control.GetVariable('$version').substring(4);
		version = version.split(',');
		version = parseFloat(version[0] + '.' + version[1]);
	    }
	    if(version > 6){
		isInstalled = true
	    }else{
	   	 isInstalled = false
	    }
	} else {
		for (i=0; i < navigator.plugins.length; i++) {
			 //loop through all the plugins installed
			
			for (i=0; i < navigator.plugins.length; i++)
			{
				var pluginIdent = navigator.plugins[i].description.toLowerCase();
				
				if(pluginIdent.indexOf('flash') > 0){
					
					arrFlash = pluginIdent.split(" ");
					
					for (j=0; j < arrFlash.length; j++){
							var FlashNum = parseFloat(arrFlash[j]);
							if(!isNaN(FlashNum)){
								version = parseFloat(FlashNum);
								 if(version > 6){
								 	
									isInstalled = true
								 }

							}

						j++;
					}

				}
		}
		}
	}
	return isInstalled;
}

function showFlash(valueIs){
	//alert("hello");
}
