/*
    Set an initial value to a textbox that is cleared on focus and reset to initial text if left blank
    Example:
            $(document).ready(function()
            {
                var iit = new inputInitialText();
                iit.bindText("#searchTerms", "Product Search");
            });
*/
function inputInitialText()
{
    var binding = this;
    
    this.elementSelector;
    this.element;
    this.text;
    
    this.bindText = function(elemSelector, txt)
                    {
                        this.elementSelector = elemSelector;
                        this.text = txt;
                        
                        this.element = $(this.elementSelector);
                        
                        this.reset();
                        this.element.bind("focus", this.clearValue);
                        this.element.bind("blur", this.reset);
                    };
                    
    this.clearValue = function()
                      {
                          if(binding.element.attr("value") == binding.text)
                          {
                              binding.element.attr("value", "");
                          }
                      };
    
    this.reset = function()
                 {
                     if(binding.element.attr("value") == "")
                     {
                         binding.element.attr("value", binding.text);
                     }
                 }
                      
    this.unbindText = function()
                      {
                          this.element.unbind("focus", this.clearValue);
                          this.element.unbind("blur", this.reset);
                      }
}


function assignment(ctrlwrap)
{
    this.control = $(ctrlwrap);
    this.moveleft = this.control.find(".moveleft");
    this.moveright = this.control.find(".moveright");
    this.left = this.control.find(".assignleft");
    this.right = this.control.find(".assignright");
    
    var assign = this;
    
    this.moveleft.click(function(e)
    {
        assign.right.children("option:selected").remove().appendTo(assign.left);
        return false;
    });
    
    this.moveright.click(function(e)
    {
        assign.left.children("option:selected").remove().appendTo(assign.right);
        return false;
    });
}

function saveSelect(SelectSelector, HiddenSelector)
{
    this.select = $(SelectSelector);
    this.hidden = $(HiddenSelector);
    
    this.v = "";
    this.options = this.select.children();
   
    ss = this;
   
    $.each(this.options, function()
    {
        if(ss.v != "")
        {
            ss.v = ss.v + ",";
        }
        ss.v = ss.v + $(this).val();
    });
    
    this.hidden.val(this.v);
}

function tooltip(elementId, elementToolTipId)
{
    this.elementId = elementId;
    this.element = $(this.elementId);
    this.elementToolTipId = elementToolTipId;
    this.elementToolTip = $(this.elementToolTipId);
    var tt = this;
    
    this.position = function(e)
    {                        
        tt.elementToolTip.show();
        tt.elementToolTip.css({
            top: e.pageY - tt.elementToolTip.offsetParent().offset().top + 5,
            left: e.pageX - tt.elementToolTip.offsetParent().offset().left + 5
        });
        
    };
    
    this.hide = function()
    {
        tt.elementToolTip.hide();
    }
    
    this.element.bind("mousemove", this.position);
    this.element.bind("mouseleave", this.hide);
}

function hideComponents(displaydiv, links)
{
    this.displayDiv = $(displaydiv);
    this.links = $(links);
    this.visibleDiv = null;
    
    var hideComp = this;
    
    this.setup = function()
    {
        this.links.each(function()
        {
            $($(this).attr("href")).appendTo(hideComp.displayDiv);
            $($(this).attr("href")).hide().find(".mainheading").remove();
        });
        
        this.links.click(function(e)
        {
            hideComp.links.each(function()
            {
                $(this).parent().removeClass("active");
            });
            
            $(this).parent().addClass("active");
            
            var selector = $(this).attr("href");
            hideComp.showComponent(selector);
            return false;
        });        
    };
    
    this.showComponent = function(componentSelector)
    {
        if(hideComp.visibleDiv != null)
        {
            if("#" + hideComp.visibleDiv.attr("id") == componentSelector)
            {
                return false;
            }
            hideComp.visibleDiv.hide();
        }
        
        $(componentSelector).show();
        hideComp.visibleDiv = $(componentSelector);
        return false;
    };
}

function rotatingBanners(divSelector, interval)
{
    this.element = $(divSelector);
    this.interval = interval;
    this.intervalHandle = null;
    
    var rotatingBanner = this;
    
    this.start = function()
    {
        this.element.children("img:gt(0)").hide();
        this.intervalHandle = setInterval($.proxy(this, "progress"), this.interval);
    };
    
    this.stop = function()
    {
        if(this.intervalHandle !== null)
        {
            clearInterval(this.intervalHandle);
            this.intervalHandle = null;
        }        
    };
    
    this.progress = function()
    {
        this.element.children(":first-child").fadeOut()
                                             .next(".banner")
                                             .fadeIn(2000)
                                             .end()
                                             .appendTo(this.element);                                             
    };
    
    this.element.hover(function(e)
    {
        rotatingBanner.stop();
    },
    function(e)
    {
        rotatingBanner.start();
    });
}

function ReplicateHTML(source, destination)
{
    this.source = $(source);
    this.destination = $(destination);
    
    this.ReplicateHTML = function()
    {            
        this.destination.append(this.source.html());
    }
}


$(document).ready(function()
{
    $("#keyCountry").change(function(e)
    {
        var div = $('#countryZoneTR');

        $.ajax({
        url: "/addresses/zones/" + this.value,
        cache: true,
        dataType: 'xml',
        async: false,
        success: function(data)
        {
            div.html("").hide();
            var countryzones = $(data).find("CountryZone");
            var element = "";
            if(countryzones.length > 0)
            {
                element = "<td>Country Zone</td><td><select name=\"objkeyCountryZone\">";
                countryzones.each(function()
                {
                    var key = $(this).children("KeyCountryZone").text();
                    var zone = $(this).children("Zone").text();
                    element += "<option value=\"" + key + "\">" + zone + "</option>";
                });
                element += "</select></td>";
            }
            else
            {
                element = "<td colspan=\"2\"><input type=\"hidden\" name=\"objkeyCountryZone\" value=\"0\" /></td>";
            }
            div.html(element).show();
        },
        error: function()
        {
            div.html("<input type='hidden' name='keyCountryZone' value='0' />");
        }
        });
    })
});

(function($){$.fn.jTruncate=function(h){var i={length:300,minTrail:20,moreText:"more",lessText:"less",ellipsisText:"...",moreAni:"",lessAni:""};var h=$.extend(i,h);return this.each(function(){obj=$(this);var a=obj.html();if(a.length>h.length+h.minTrail){var b=a.indexOf(' ',h.length);if(b!=-1){var b=a.indexOf(' ',h.length);var c=a.substring(0,b);var d=a.substring(b,a.length-1);obj.html(c+'<span class="truncate_ellipsis">'+h.ellipsisText+'</span>'+'<span class="truncate_more">'+d+'</span>');obj.find('.truncate_more').css("display","none");obj.append('<div class="clearboth">'+'<a href="#" class="truncate_more_link">'+h.moreText+'</a>'+'</div>');var e=$('.truncate_more_link',obj);var f=$('.truncate_more',obj);var g=$('.truncate_ellipsis',obj);e.click(function(){if(e.text()==h.moreText){f.show(h.moreAni);e.text(h.lessText);g.css("display","none")}else{f.hide(h.lessAni);e.text(h.moreText);g.css("display","inline")}return false})}}})}})(jQuery);
