/**
 * tools to enable microsites to do their thing
 *
 */

// retailer data - object not array! String for flash is created down the bottom
var retailerUrl = {
                "ace_cameras":"http://www.acecameras.co.uk/asp/web/recorprod/1/product/13540/cat/1274/ph/cat/keywords//recor/1/SearchFor//PT_ID/1/product.asp",
                "amazon":"http://www.amazon.co.uk/s/ref=nb_ss_ce/203-5262714-6775937?url=search-alias%3Delectronics&field-keywords=Fujifilm+FinePix+z20fd&x=0&y=0",
                "ask_direct":"http://www.askdirect.co.uk/uview?call=ask.productBrowse&id=466&make=Fuji",
                "bennetts":"http://www.bennettsonline.co.uk/category.asp?search=FUJI&filterbrand=65#page:1|topresults",
                "best_cameras":"http://www.bestcameras.co.uk/shop/acatalog/Fuji_FinePix_Z20fd.html",
                "boots":"http://www.boots.com/webapp/wcs/stores/servlet/CatalogSearchResultView?storeId=10052&catalogId=11051&langId=-1&pageSize=12&beginIndex=0&sType=SimpleSearch&resultCatEntryType=2&searchTerm=z20fd&x=0&y=0",
				"camera_world":"http://www.cameraworld.co.uk/ViewProdDetails.asp?prod_code=PON08D000007&Prod_name=Fujifilm+FinePix+Z20fd+&CAT_CODE=5&SUBCAT_CODE=52",
                "currys":"http://www.currys.co.uk/martprd/store/cur_page.jsp?page=Product&fm=null&sm=null&tm=null&sku=317232&category_oid=-33706",
//                "digistor":"http://www.digistor.co.uk/?lid=61&make=429&price=",
                "digital_camera_exchange":"http://www.digitalcameraexchange.co.uk/products.asp?CAT_CODE=4&SUBCAT_CODE=54&cat_Name=Digital%20Cameras&categoryname=Digital+Cameras%20%20-%20Fuji",
				"dixons":"http://www.dixons.co.uk:80/martprd/store/dix_page.jsp?BV_EngineID=ccgdadeffjigjhkcflgceggdhhmdgmi.0&page=SimpleSearchProducts&low_bound=0&up_bound=0&criterion=z20fd",
				"focus_on_price":"http://www.focusonprice.com/cart.php?target=search&substring=z20",
//                "harpers":"http://www.harpersphoto.co.uk/dcam.html#fuji",
                "park_cameras":"http://www.parkcameras.com/SearchResults/mcs/txtSearch/z20fd/brand/Fujifilm/category/0/catname/--%20Category%20--/",
                "play":"http://www.play.com/Search.aspx?searchtype=ELEC&searchstring=z20fd&page=search&pa=search&go.x=0&go.y=0",
                "ja_cameras":"http://www.jandacameras.co.uk/shop/index.php?searchStr=z20fd&act=viewCat&Submit=Go",
                "jacobs":"http://www.jacobsdigital.co.uk/index.php?thequery=z20fd",
                "jessops":"http://www.jessops.com/Products/SearchResults.aspx?searchword=z20fd",
                "john_lewis":"http://www.johnlewis.com/Search/Search.aspx?SearchTerm=z20fd",
                "kingsley_photographic":"http://www.kingsleyphoto.co.uk/fuji_index.php",
				"tesco":"http://direct.tesco.com/search/default.aspx?search=z20fd",
                "warehouse_express":"http://www.warehouseexpress.com/category/basecategory.aspx?cat03=3064&Brand=84&Range=978#first"
        };

// DOM ready stuff
$(document).ready(function(){
	$("#frmSend").ajaxSubmit();
});

$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var params = {};
	$(this)
	.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
	.filter(":enabled")
	.each(function() {
	  params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	});
	$("body").addClass("curWait");
		$.post(this.getAttribute("action") + "?call=ajax", params, function(xml){
			$("body").removeClass("curWait");
			strError = "Unable to submit form. Please try again later.";
			oFocus = null;
			$("AjaxResponse", xml).each(function() {
				strError = this.getAttribute("response");
			});
			if (strError == 'SUCCESS!') {
				// hide the form
				$(".formErrors").html("Thankyou, your friend has been emailed.").filter(":hidden")
					.fadeIn("normal")
					.animate({opacity: 1.0}, 1000)
					.fadeOut("normal");
				pageTracker._trackPageview('send2friend-sent');
			} else {
				// display error
				$(".formErrors").html(strError).filter(":hidden")
					.slideDown("normal")
					.animate({opacity: 1.0}, 3000)
					.slideUp("normal");
			}
		});
		return false;
	});
	return this;
}

function gamePopup(colour) {
	h = window.open('game.php','gameWindow',
'width=900,height=560,toolbar=0, location=0, status=0, menubar=0, scrollbars=0 ,resizable=0');
	 if (window.focus) {h.focus();}
	return false;
}

function wtbRefer(retailer) {
	pageTracker._trackPageview('retailer/' + retailer);
	if (!retailerUrl[retailer]) {
		alert('Unknown retailer: ' + retailer);
	} else {
		 h = window.open(retailerUrl[retailer],"retailerWindow");
		 if (window.focus) {h.focus();}
	}
	return false;
}

// create a randomised list of not more than 20 names to pass to flash
var retailerArray = []; // holding area for list of names
for (var shop in retailerUrl) {	retailerArray.push(shop); }	// populate array
Array.prototype.shuffle = function (){ for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp); };
retailerArray.shuffle(); //mix them up
if (retailerArray.length > 20) { retailerArray.length = 20;} // ensure there's only 20
retailerList = retailerArray.join(","); // make a string
