﻿gCjs.namespace("harrods.block");

gCjs.harrods.block.brandPage = function(id) {
    this._container = $("#" + id);
}

var a = gCjs.harrods.block.brandPage;

a.prototype.init = function() {
    gCjs.log(this._container);

    var _self = this;
    this.link_name;
    $("#brands_tabs").tabs();
    $.address.change(function(event) {
        if (event.value != '/') {
            _self.action = event.pathNames[0];
            _self.letter = event.parameters.letter;
            _self.name = event.parameters.name;
            _self.department = event.parameters.department;
            _self[_self.action]();
        } else {
            $.address.value('/SearchByName');
            $.address.title($.address.title().split(' | ')[0] + ' | Search by name');
        }
    });

    //this.getFeaturedBrands();
    this.getBrandDepartments();
    this.autoComplete();
    this.brandsSearch();
}
a.prototype.SearchByName = function() {
    var _self = this;
    var theTitle = $('a[rel=address:/' + this.action + ']');
    var enterName = 'Please enter a brand name in the search field.';
    if (this.name == undefined && this.department == undefined) {
        $.address.title($.address.title().split(' | ')[0] + ' | ' + theTitle.text());
        
        $("#brands_tabs > li:first a").click();
        $("#brand_name").focus().val('');
        $("#brands_list > li").remove();
    } else if (this.name == '' && this.department != undefined) {
        $.address.title($.address.title().split(' | ')[0] + ' | ' + theTitle.text());
        
        $('#brands_tabs').tabs({ selected: 0 });
        $('#viewAtoZ').hide();
        if ($(".brands_preview > .notice").text() != enterName) {
            $(".brands_preview > .notice").append(enterName).show();
            $("#brand_name").focus();
            $("#brand_name").focus().val('');
            $("#brands_list > li").remove();
        } else {
            $("#brand_name").focus();
        }
    } else if (this.name != undefined && this.department != undefined) {
        $('#brands_tabs').tabs({ selected: 0 });
        $('#viewAtoZ').hide();
        $("#brand_name").val(this.name);
        $("#brands_departments").val(this.department);
        _self.brandsSearch2();
    }
}
a.prototype.brandsSearch = function() {
    var _self = this;
    $("#submit_search_brand").click(function() {
        var thename = $("#brand_name").val();
        var thedepartment = $("#brands_departments").val();
        $.address.value('/SearchByName/?department=' + thedepartment + '&letter=&name=' + thename);
        return false;
    })
}
a.prototype.brandsSearch2 = function() {
    var _self = this;
    $.address.title($.address.title().split(' | ')[0] + ' | Search for ' + this.name);
    $(".brands_preview > .notice").text('');
    $("#brands_list > li").remove();
    $(".loader").show();

    gCjs.util.ServiceProxy.getService("BrandServices.asmx").call(
    "BrandSearch",
    new gCjs.util.Delegate(this, _self.onBrandSearch),
    { searchText: this.name, department: this.department }
    );
    
    //for adding omniture data
    //_self.sendOmnitureData();
    return false;
}
a.prototype.onBrandSearch = function(response) {
    var _self = this;
    var searchText = $("#brand_name").val();
    if (response.d.length < 1) {
        $(".brands_preview > .notice").append('Sorry, no brand with the name: "' + searchText + '"').show();
    } else {
    for (var i in response.d) {
            if (response.d[i].DetailPageUrl != undefined) {
                $("#brands_list").append('<li class="clearfix"><p>YOUR SELECTED BRAND</p><span class="brand_prev_name">' + response.d[i].DisplayName + '</span><div style="display:block;"><p class="prev_text">' + response.d[i].PreviewText + '</p><a href="' + response.d[i].DetailPageUrl + '" title="Find out more about ' + response.d[i].DisplayName + '" class="find_more">FIND OUT MORE&nbsp;»</a><p class="enquiries"><strong>Harrods Store Enquiries:</strong><br />+44 (0) 20 7730 1234</p></div></li>');
            } else {
            $("#brands_list").append('<li class="clearfix"><p>YOUR SELECTED BRAND</p><span class="brand_prev_name">' + response.d[i].DisplayName + '</span><div style="display:block;"><p class="prev_text">' + response.d[i].PreviewText + '</p><p class="enquiries"><strong>Harrods Store Enquiries:</strong><br />+44 (0) 20 7730 1234</p></div></li>');
            }
        }
        //for adding omniture data
        _self.sendOmnitureData();
    }
    $(".loader").hide();
}
a.prototype.viewAtoZ = function() {
    var theTitle = $('a[rel=address:/' + this.action + ']');
    $.address.title($.address.title().split(' | ')[0] + ' | ' + theTitle.text());
    
    $('#brands_tabs').tabs({ selected: 1 });
    $('#SearchByName').hide();
    
    $(".atoz_lists > p").text('');
    $(".atoz_lists > .loader").hide();
    $(".brands_preview > .notice").text('');
    $("#brands_list > li").remove();
    $('.brands_atoz').remove();
    if ($('.jcarousel-skin-tango').length >0) {
        $('.jcarousel-skin-tango').remove();
    }
    $(".brands_atoz_list > li > a").removeClass('active');
}
a.prototype.autoComplete = function() {
    $("#brand_name").autocomplete("/harrodsstore/Services/CommonServices.asmx/GetAutoCompleteData", {
        extraParams: { "dataFor": '"BRANDS"'}
        }).focus();
}
a.prototype.getFeaturedBrands = function() {
    gCjs.util.ServiceProxy.getService("BrandServices.asmx").call(
    "GetFeaturedBrands",
    new gCjs.util.Delegate(this, this.onFeaturedBrands),
    {}
);
}
a.prototype.onFeaturedBrands = function(response) {
    for (var i in response.d) {
        $("#FeaturedBrands > ul").append('<li><a href="' + response.d[i].DetailPageUrl + '"><img src="' + response.d[i].ImageUrl + '" alt="' + response.d[i].DisplayName + '" /></a></li>');
    }
}
a.prototype.getBrandDepartments = function() {
    gCjs.util.ServiceProxy.getService("BrandServices.asmx").call(
    "GetBrandDepartments",
    new gCjs.util.Delegate(this, this.onBrandDepartments),
    {}
);
}
a.prototype.onBrandDepartments = function(response) {
    for (var i in response.d) {
        $("#brands_departments").append('<option value="' + response.d[i].Value + '">' + response.d[i].Text + '</option>');
    }
}

a.prototype.atozSearch = function() {
var _self = this;
    $('#brands_tabs').tabs({ selected: 1 });
    $('#SearchByName').hide();
    $.address.title($.address.title().split(' | ')[0] + ' | Search by name starting with ' + this.letter);

    $('a[rel=address:/atozSearch/?department=ALL&letter=' + this.letter + '&name=]').addClass('active');
    $('a[rel=address:/atozSearch/?department=ALL&letter=' + this.letter + '&name=]').parent('li').siblings('li').children('a').removeClass('active');

    $(".atoz_lists > p").remove();
    
    $(".brands_preview > .notice").text('');
    $("#brands_list > li").remove();
    $('.brands_atoz').remove();
    
    

    gCjs.util.ServiceProxy.getService("BrandServices.asmx").call(
                "BrandSearch",
                new gCjs.util.Delegate(this, _self.onatozSearch),
                { searchText: this.letter, department: 'ALL' }
                );
    $('.jcarousel-skin-tango').hide();            
    $(".atoz_lists > .loader").show();      
    return false;

}
a.prototype.onatozSearch = function(response) {
    var _self = this;

    if (response.d.length == 0) {
        $('.jcarousel-skin-tango').remove(); 
        //$('.atoz_lists').height('28px');
        $(".atoz_lists").append('<p>Sorry, no brand starting with the letter: "' + this.letter + '"</p>')
    } else {
        this.listLetter = response.d[0].DisplayName.substr(0, 1).toUpperCase();
        var k = 1;
        var jc = 1;
        $('.jcarousel-skin-tango').remove(); 
        var brands_atoz = $('<ul class="brands_atoz jcarousel-skin-tango" id="brand_carousel"></ul>');
        var brands_atoz_in;

        $(".atoz_lists").append(brands_atoz);

        for (var i in response.d) {
            if (k == 1) {
                brands_atoz_in = $('<ul class="brands_atoz_in"></ul>');
            }
            var li = $('<li><a href="#atozResultSearch/?department=' + this.department + '&letter=' + this.listLetter + '&name=' + response.d[i].DisplayName + '" rel="address:/atozResultSearch/?department=' + this.department + '&letter=' + this.listLetter + '&name=' + response.d[i].DisplayName + '">' + response.d[i].DisplayName + '</a></li>');
            brands_atoz_in.append(li);

            if (k == 7) {
                var li2 = $('<li></li>').append(brands_atoz_in)
                brands_atoz.append(li2);
                k = 1;
                jc++;
            } else {
                k++;
            }
        }
        var li1 = $('<li></li>').append(brands_atoz_in)
        brands_atoz.append(li1);

        if (jc >= 6) {
            //$('.atoz_lists').height('150px');
            $("#brand_carousel").jcarousel();
            $(".jcarousel-next").text('Next');
            $(".jcarousel-prev").text('Prev');
        }

    }
    $(".loader").hide();
    if (this.name != '') {
        _self.atozResultSearch2();
    }
}
a.prototype.atozResultSearch = function() {
    var _self = this;
    if (this.listLetter == this.letter) {
        _self.atozResultSearch2();
    } else {
        _self.atozSearch();
    }
}
a.prototype.atozResultSearch2 = function() {
    var _self = this;
    $('#brands_tabs').tabs({ selected: 1 });
    $('#SearchByName').hide();
    $.address.title($.address.title().split(' | ')[0] + ' | Search for ' + this.name);
    $('a[rel=address:/atozSearch/?department=ALL&letter=' + this.letter + '&name=]').addClass('active');
    $('a[rel=address:/atozSearch/?department=ALL&letter=' + this.letter + '&name=]').parent('li').siblings('li').children('a').removeClass('active');
    $('.brands_atoz_in').children('li').children('a').removeClass('active');
    $('.brands_atoz_in').children('li').children('a[rel=address:/atozResultSearch/?department=' + this.department + '&letter=' + this.listLetter + '&name=' + this.name + ']').addClass('active');

    var searchText = $('a[rel=address:/atozResultSearch/?name=' + this.name + ']').text();
    $(".brands_preview > .notice").text('');
    $("#brands_list > li").remove();
    $(".brands_preview > .loader").show();

    gCjs.util.ServiceProxy.getService("BrandServices.asmx").call(
            "BrandSearch",
            new gCjs.util.Delegate(this, _self.onatozResultSearch),
            { searchText: this.name, department: 'ALL' }
            );
    //for adding omniture data
    //_self.sendOmnitureData();
    return false;
}
a.prototype.onatozResultSearch = function(response) {
    var _self = this;
    //var searchText = $("#brand_name").val();
    if (response.d.length < 1) {
        $("#brands_list > .notice").append('Sorry, no brand with the name: "' + this.name + '"');
    } else {
    for (var i in response.d) {
            if (response.d[i].DetailPageUrl != undefined) {
                $("#brands_list").append('<li class="clearfix"><p>YOUR SELECTED BRAND</p><span class="brand_prev_name">' + response.d[i].DisplayName + '</span><div style="display:block;"><p class="prev_text">' + response.d[i].PreviewText + '</p><a href="' + response.d[i].DetailPageUrl + '" title="Find out more about ' + response.d[i].DisplayName + '" class="find_more">FIND OUT MORE&nbsp;»</a><p class="enquiries"><strong>Harrods Store Enquiries:</strong><br />+44 (0) 20 7730 1234</p></div></li>');
            } else {
            $("#brands_list").append('<li class="clearfix"><p>YOUR SELECTED BRAND</p><span class="brand_prev_name">' + response.d[i].DisplayName + '</span><div style="display:block;"><p class="prev_text">' + response.d[i].PreviewText + '</p><p class="enquiries"><strong>Harrods Store Enquiries:</strong><br />+44 (0) 20 7730 1234</p></div></li>');
            }
        }
        //for adding omniture data
        _self.sendOmnitureData();
    }
    $(".loader").hide();
}

//for adding omniture data
a.prototype.sendOmnitureData = function() {
        //omniture code comes over here
        s.pageName = this.name + ":BrandInfoPage";
        /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
        var s_code = s.t(); if (s_code) document.write(s_code);

        //alert(s.pageName);
}

gCjs.register("block_brandPage", a);