/*
* name: fsnm.ui.js
* author: RAJF / IP
* notes: global ui functions
*/

/*
* ## CONFIG ##
*/
// Set to false for Live!
//code for debug:- if(_debug) debug("test");
var _debug = true;
// is MyAmtico pan opened
var _MyAmtico = true;
// is user logged in. Set this in page via script written out from server
var _loggedIn = false;
// web service image path
var _productImagePath = "/images/content/products/";
//var _webServiceUrl = "https://2009.amtico.dev.whistler.internal/WebServices/Membership.asmx?op=Login";
var _webServiceUrl = "/Membership.asmx";
// Enable browser caching of search HTML.
var _cacheIncludes = false;
// lightbox settings
_lightboxSettings = jQuery.extend({
	overlayBgColor: '#000',
	overlayOpacity: 0.7,
	imageLoading: '/images/site/lightbox/loading.gif',
	imageBtnClose: '/images/site/lightbox/lightbox-btn-close.gif',
	imageBtnPrev: '/images/site/lightbox/lightbox-btn-prev.gif',
	imageBtnNext: '/images/site/lightbox/lightbox-btn-next.gif',
	imageBlank: '/images/site/lightbox/lightbox-blank.gif'
});
/*
* ## END CONFIG ##
*/

/*
* window onload function
*/
$(function(){
	setupMyAmtico();
	setLinkRel();
	$("#logo").pngFix();
	//if ($("#product-search").length > 0) {
	//    setProductSearch();
	//}
});

/*
* show loader
*/
function showLoader()
{
	$("#my-amtico").empty().prepend('<div id="loader"><img src="/images/site/loader_ani.gif" alt="loading"/></div>');
}

/*
* hide loader
*/
function hideLoader()
{
	$("#loader").remove();
}

/*
* show my amtico
*/
function showMyAmtico()
{
	if(_debug) debug("showMyAmtico");
	
	if($("#my-amtico").is(":not(:hidden)"))
	{
		if(_debug) debug("visible");
		$("#my-amtico").hide();
	}
	
	$("#my-amtico").slideToggle("normal", function () {
    $("#my-amtico-button").find(".arrow").attr("src", "/images/site/buttons/arrow_up.gif");
  });
}

/*
* hide my amtico
*/
function hideMyAmtico()
{
	
if(_debug) debug("hideMyAmtico");
/*
	$("#my-amtico").slideToggle("normal", function () {
		//$("#my-amtico").html("");
		$("#my-amtico-button").find(".arrow").attr("src", "/images/site/buttons/arrow_down.gif");
  });*/
  $("#my-amtico-button").find(".arrow").attr("src", "/images/site/buttons/arrow_down.gif");
  //$("#my-amtico").html("").css("display","none");
  $("#my-amtico").css("display","none");
}

/*
* set my amtico arrow
*/
function setMyAmticoArrow(direction)
{
	if(_debug) debug("setMyAmticoArrow: " + direction); 
	if ($("#my-amtico-button").find(".arrow").length > 0)
	{
		$("#my-amtico-button").find(".arrow").attr("src", "/images/site/buttons/arrow_" + direction.toLowerCase() + ".gif");
	}
	else
	{
		var arrow = new Image();
		arrow = $(arrow);
		arrow.attr("class", "arrow");
		arrow.attr("src", "/images/site/buttons/arrow_down.gif");
		arrow.attr("alt", "");
		$("#my-amtico-button").append(arrow);
	}
}

/*
* does what it says on the tin
*/
function setupMyAmtico()
{
	setMyAmticoArrow();
	
	///$("#my-amtico-button").after("<div id=\"my-amtico\"></div>");
	$("#my-amtico").css("display","none");
	//add toggle
	$("#my-amtico-button").click(function() {
	    $("#my-amtico").removeAttr("style");
	    if (_MyAmtico==true) 
	    {
	        showMyAmtico();
	        _MyAmtico=false;
//	        if (1 == 1) 
//	        {
//	            // got and get product list
//	            ///showLoader();
//	            showMyAmtico();
//	            ///loadHtml($("#my-amtico"), "/includes/myamtico/loggedin.htm", function() {
//	            ///    hideLoader();
//	            ///});
//	        }
//	        else 
//	        {
//	                hideMyAmtico();
//	                setMyAmticoArrow("up");
//	                ///showLoader();
////	                loadHtml($("#my-amtico"), "/includes/myamtico/login.htm", function() 
////	                {
////	                    setMyAmticoArrow("up");
////	                    $("#login input[@type=submit]").click(function() 
////	                    {
////	                        if (_debug) debug("submited");
////	                        data = "language=en-gb&email=robert.ford@fsnm.co.uk&password=12345678";
////	                        handleRequest(_webServiceUrl + "?op=Login", data, processLogin);
////	                        return false;
////	                    });
////	                });
//	        }
	    }
	    else 
	    {
	        hideMyAmtico();
	        _MyAmtico=true;
	    }
	    return false;
	});
}

/*
* handle results from login call to server
*/ 
function processLogin(xmlDataSet)
{
	//handle xml dataset aka top marques and render out products
}

/*
* open windows in external window
*/ 
function setLinkRel()
{
  $('a[rel="external"]').click( function() {
      window.open( $(this).attr('href') );
      return false;
  });
}

function hideShow()
{
  var showText = "show entire collection"
  var hideText = "hide collection"
  
  $("ul.product-chip").prev("h2.triple-column").append("<a href=\"#\">"+showText+"</a>").addClass("click-area");
  $("h2.triple-column").next("ul.product-chip").addClass("hidden");

  $("h2.click-area").click(function() { 
    if($(this).next("ul").hasClass("hidden"))
    {
      $(this).next("ul").removeClass("hidden");
      $(this).children("a").empty()
      $(this).children("a").append(hideText)
    } else {
      $(this).next("ul").addClass("hidden");
      $(this).children("a").empty()
      $(this).children("a").append(showText)
    };
    return false;
  });
}


function fsnmImageSwitch(thumbnailElements,mainImageElement)
{
	$(thumbnailElements).click(function()
	{
		$(mainImageElement).find("img").attr("src",$(this).attr("href"));
		return false;
	});
}

function fsnmGallery(lightBoxElement, thumbnailElements, mainImageElement)
{
	$(lightBoxElement).lightBox(_lightboxSettings);
	
	$(thumbnailElements).click(function()
	{
		if(mainImageElement != "#enlargeImageLink"){
		  $(mainImageElement).attr("href",$(this).parent().attr("href"));
		  $(mainImageElement).find("img").attr("src",$(this).attr("src"));
		} else {
		  $(mainImageElement).attr("href",$(this).attr("href"));
		  $(mainImageElement).find("img").attr("src",$(this).find("img").attr("src"));
		}
		return false;
	});
}



function fsnmChangeProduct(productID)
{
  window.location="productdetail.aspx?productid="+productID
}



