// for the google custom serach engine search box
function convert(mySel) {
	// first i check to see if they've entered something besides
	// Search Our Site - this prevents people from submitting the
	// default value as a search term
	if (mySel.value == "Search Our Site") {
		return false;
	} else {
		// declaring variables
		var myWin, myVal, gcseCode;
		// Google gives each of its custom search engines an id, so
		// we need to set the variable that holds this value based on
		// which domain we're on
		if (location.host.indexOf("cosmeticsandtoiletries") > 0) {
			gcseCode = "xza5h_cqlz8";
			siteURL = "www.cosmeticsandtoiletries.com";
		} else if (location.host.indexOf("gcimagazine") > 0) {
			gcseCode = "orvhyiscjqi";
			siteURL = "www.gcimagazine.com";
		} else if (location.host.indexOf("perfumerflavorist") > 0) {
			gcseCode = "0xaqj4wjnme";
			siteURL = "www.perfumerflavorist.com";
		} else if (location.host.indexOf("skininc") > 0) {
			gcseCode = "qhcvuq8a7qo";
			siteURL = "www.skininc.com";
		}
		// set the search terms to a variable
		myVal = mySel.value;
		// do some replacing to make sure the url looks ok
		myVal = myVal.replace(/ /g,"+");
		myVal = myVal.replace(/"/g,"%22");
		myVal = myVal.replace(/&/g,"%26");
		// now we add the url we'll be directing them to with the
		// search terms
		myVal = "http://" + siteURL + "/search?cx=012659430018965267705%3A" + gcseCode + "&cof=FORID%3A11&q=" + myVal;
		// this next part i don't really understand, but clearly
		// we're using myVal to set myWin and then going there
		if (myVal) {
			if(mySel.form.target)myWin = parent[mySel.form.target];
			else myWin = window;
			if (! myWin) return true;
			myWin.location = myVal;
		}
		return false;
	}
}

// expand-collapse stuff
function toggleDiv(){
	var hiddenDiv = document.getElementById('collapse');
	var toggleLink = document.getElementById('toggleLink');

	if (hiddenDiv.style.display == "none") {
		hiddenDiv.style.display = "block";
		toggleLink.style.backgroundImage="url(../../css/magSites/designImages/collapse_icon.gif)";
		toggleLink.innerHTML = "collapse";
	}
	else {
		hiddenDiv.style.display = "none";
		toggleLink.style.backgroundImage="url(../../css/magSites/designImages/expand_icon.gif)";
		toggleLink.innerHTML = "expand";
	}
}

function closeDevDiv() {
	document.getElementById('devHeader').style.display = "none";
}

// for the search form on the directory solution
function refineSearch(){
	document.getElementById('searchForms').style.display = "block";
	// this is the p tag
	var refineSearchPara = document.getElementById('refineSearch');
	// this is the a tag
	var refineSearchLink = document.getElementById('refineSearchLink');
	// this is the text "Refine Your Search"
	var refineLinkString = refineSearchLink.innerHTML;
	refineSearchPara.removeChild(refineSearchLink);
	refineSearchPara.innerHTML = "<span>" + refineLinkString + "</span>";
}

function createMaterialAds(materialAds) {
//	alert(materialAds.length);
	var adColumn = document.getElementById('col3_content');
//	alert(skip);
	for (var i = 0; i < materialAds.length; i++) {
//		alert(i);
		var ad = materialAds[i].ffm_material_ad;
//		alert(ad.ad_text);
		var advertiser = ad.advertiser;
//		alert(advertiser.logo_path);
		var adLink = document.createElement('a');
		adLink.href = adServerURL + '/click/ffm_mat/' + ad.id + '/?q=' + query + '&t=' + materialType + '&s=' + skip;
//		alert(adLink.href);
		adLink.id = 'keywordAdLink';
		
		var adImg = document.createElement('img');
		adImg.src = adServerURL + advertiser.logo_path;
		adLink.appendChild(adImg);
		
		var adH4 = document.createElement('h4');
		var adH4Text = document.createTextNode('Your source for:');
		adH4.appendChild(adH4Text);
		adLink.appendChild(adH4);
		
		var adPara = document.createElement('p');
		var adParaText = document.createTextNode(ad.ad_text);
		adPara.appendChild(adParaText);
		adLink.appendChild(adPara);
		
		adColumn.appendChild(adLink);
	}
}

function createFeaturedAds(featuredAds) {
//	skip = false;
//	alert(featuredAds.length);
	// first thing is to check if the searchForms div exists, becuase that will determine where we append the featuredMaterials div later
	var searchFormsDiv = document.getElementById('searchForms');
	var searchFormsBreak = document.getElementById('searchAgainClear');
	
	// then we locate the col2_content div and create the div that will hold all the featured material ads
	var contentDiv = document.getElementById('col2_content');
	var featuredDiv = document.createElement('div');
	featuredDiv.id = 'featuredMaterials';
	
	// next we create the heading and append it to the featuredMaterials div
	var featuredHeading = document.createElement('h2');
	var featuredHeadingText = document.createTextNode('Featured Materials');
	featuredHeading.appendChild(featuredHeadingText);
	featuredDiv.appendChild(featuredHeading);
	
	// create the list of the featured materials
	var featuredList = document.createElement('ul');
	
	// now we loop through the matches that came in and create the li's for the list
	for (var i = 0; i < featuredAds.length; i++) {
		var ad = featuredAds[i].ffm_featured_ad;
//						alert(ad.material);
		var featuredItem = document.createElement('li');
		var featuredLink = document.createElement('a');
		featuredLink.href = adServerURL + "/click/ffm_feat/" + ad.id + "/?t=" + featuredType + '&s=' + skip;
		var featuredItemText1 = document.createTextNode(ad.material);
		var featuredBreak = document.createElement('br');
		var featuredItemText2 = document.createTextNode(ad.ad_text);
		featuredLink.appendChild(featuredItemText1);
		featuredItem.appendChild(featuredLink);
		featuredItem.appendChild(featuredBreak);
		featuredItem.appendChild(featuredItemText2);
		featuredList.appendChild(featuredItem);
	}
	// now we're ready to append the list to the featuredMaterials div
	featuredDiv.appendChild(featuredList);
	
	// and finally, we just figure out where we're appending the featuredMaterials div and we're done
	if (searchFormsDiv) {
		searchFormsDiv.insertBefore(featuredDiv, searchFormsBreak);
	} else {
		contentDiv.appendChild(featuredDiv);
	}
}

var previewPageCount;
// http://ecs.stage.dir.perfumerflavorist.com/detail/ffmMaterial.html?mid=3770
// http://ecs.stage.dir.perfumerflavorist.com/logout.do
function show_preview_note(count) {
	previewPageCount = count;
	var page_count = count == 1 ? count + ' page' : count + ' pages';
	var note = '<div id="previewNote"><h2>' + page_count + ' remaining</h2><p>You are previewing the directory, please <a id="logInLink" href="/">log in</a> if you have an account or <a id="signupLink" href="/register/buyer.html">signup</a>.</p></div>';
	var login_form = '<div id="loginForm" style="display: none;"><form action="/login.do" method="post" name="loginform"><input type="hidden" name="success" value="' + window.location.pathname + window.location.search + '" /><input type="hidden" name="fail" value="main/login.html" /><label for="uname">Email Address</label><input type="text" id="uname" class="text" name="uname" /><label for="pword">Password</label><input type="password" id="pword" class="text" name="pword" /><input type="submit" class="submit" value="Sign In" /></form><p><a href="/main/forgot.html">Forgot your password?</a></p></div>';
	var signup_form = '<div id="signupForm" style="display: none;"><form action="/register/buyer.html" method="get"><label for="firstName">First Name</label><input type="text" class="text" name="firstName" value="" id="firstName" /><label for="lastName">Last Name</label><input class="text" type="text" name="lastName" value="" id="lastName" /><label for="companyName">Company</label><input class="text" type="text" name="companyName" value="" id="companyName" /><label for="emailAddress">Email</label><input class="text" type="text" name="emailAddress" value="" id="emailAddress" /><input type="submit" class="submit" value="Sign Up" /></form><p id="signupNote">Membership is $195/year, <a href="/main/login.html#options">multiuser memberships</a> also available!</p></div>';
	if (count == 0) { note = '<div id="previewNote"><h2>This is the last page in your preview</h2><p>Please <a id="logInLink" href="/">log in</a> or <a id="signupLink" href="/register/buyer.html">signup</a> to continue.</p></div>' };
	$('div#col1_content').prepend(note + login_form + signup_form);
}

function fadeInPreviewNote() {
	$('#previewNote').fadeIn(400);
	if (previewPageCount == 0) { $('#col1_content #signupForm').fadeIn(400) };
	clearTimeout(fadeInPreviewNote);
}

$(document).ready(function() {
	$('#previewNote a#logInLink').click(function(e) { e.preventDefault(); $('div#signupForm').hide(); $('div#loginForm').slideDown(400, function() {$('#uname').focus() })});
	$('#previewNote a#signupLink').click(function(e) { e.preventDefault(); $('div#loginForm').hide(); $('div#signupForm').slideDown(400, function() {$('#firstName').focus() })});
	$('div#searchResults table.tablesorter tbody tr:odd').addClass('even');
	if (previewPageCount <= 4) { setTimeout(fadeInPreviewNote, 800) };
}); // end of document ready function
