/*
* 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 user logged in. Set this in page via script written out from server
var _loggedIn = true;
// 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;
/*
* ## END CONFIG ##
*/

/*
* window onload function
*/
$(function() {
    formTextRemove();
    setLinkRel();
    
    if ($("#product-search").length > 0) {
        setProductSearch();
    }
    $('body').addClass("jse");
    setFocusOutletSearch();
    setFooterBlockLink();
});

function pageLoad() {
    searchMoreInfoHideShow();
    listingHideShow();
}

/*
* 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();
}

/*
* 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 searchMoreInfoHideShow() {
    $("div.more-info-div").hide();
    $("div.column-header a.more-info-link").click(function(event) {
        event.preventDefault();
        if ($(this).text() == "more information -") {
            $(this).text("more information +");
        }
        else {
            $(this).text("more information -");
        }
        $(this).parent().parent().next(".more-info-div").slideToggle("slow");
    });
}

function fsnmImageSwitch(thumbnailElements,mainImageElement)
{
	$(thumbnailElements).click(function()
	{
		$(mainImageElement).find("img").attr("src",$(this).attr("href"));
		return false;
	});
}

function fsnmGallery(lightBoxElement, thumbnailElements, mainImageElement)
{
    $(lightBoxElement).colorbox({ opacity: .7 });
	
	$(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
}

function setProductSearch()
{
  var fsNum = $(".form-columns").find("fieldset");// creates object
  
  fsNum.each(function(index, domEle){ // loops though fieldset elements      
    if(index<(fsNum.length-1)){ // Applies the following to all fieldset elements except the last one
      $(domEle).children().hide(); // hides all child elements of the fieldset tag          
      $(domEle).css("border","none").find("legend").show().find("span").append("<a href=\"#\">Change</a>"); //shows the legend and adds a link
      $(domEle).find("a").click(function(){ //on click of the link
        fsNum.each(function(i,de){// loops though fieldset elements 
          if(i>index){// selects only the fieldsets lower that clicked fieldset 
            $(de).hide(); // hides fieldset
          }
        })
        $(domEle).removeAttr("style").children().show(); // remove style and shows clicked fieldset
        return false // stops callback
      })
    }
  })
}

function formTextRemove() {
    $("div#modal_form.samplesPopupForm div.postcard_form input[type='text']").click(function() {
        var text = $(this).val();
        var tooltip = $(this).attr("title");
        if (text == tooltip) {
            $(this).val("");
        }
    });
}

function listingHideShow() {
    $('div.listItemBody').hide();
    $('div.listItemHeader').append('<div class="last hideShow maximise">More information</div>')
    $('div.listItemHeader .last').click(function() {
        $(this).toggleClass('maximise').parent().siblings('div.listItemBody').slideToggle()
        return false;
    });
}

function setFocusOutletSearch() {
    $("#ctl00_ctl00_ContentPlaceHolderContent_ContentPlaceHolderContent_residentialSearchBox_txtPostcode").keypress(function(event) {
        if (event.which == 13) {
            $("input.cta_submitbutton").click();
        }
    });
}

function setFooterBlockLink() {
    var blocks = $('#footer .block');
    if (blocks.length > 0) {
        blocks.each(function(index) {
            var blockLink = $(this).find('a');
            var blockHeader = $(this).find('h2');
            if (blockLink.length > 0) {
                blockHeader.eq(0).wrapInner('<a title="' + blockLink.eq(0).attr('title') + '" href="' + blockLink.eq(0).attr('href') + '"></a>');
            }
        });
    }
}


