﻿var search_text = "";

$(document).ready(function(){
    search_text = $('.searchBox').text();
    $('.searchInput').Watermark(search_text);
});



//a widget to be able to search products from anywhere
$(document).ready(function(){
    
    $('.widgetProductSearch').find('.widgetSearchButton').bind('click', function(){
        var url = $(this).attr('href') + '?act=search';
        var searchBox = $(this).parent('.widgetProductSearch').find('.widgetSearchBox');
        window.location = url + '&term=' + searchBox.val();
        
        return false;
    });
    
    $('.widgetProductSearch .widgetSearchBox').keyup(function(e){
        if(e.keyCode == 13){
            var url = $(this).parent('.widgetProductSearch').find('.widgetSearchButton').attr('href') + '?act=search';
            var newUrl = url + '&term=' + $(this).parent('.widgetProductSearch').find('.widgetSearchBox').val();
            window.location = newUrl;
        }
    });
});


//gives backToSearch functionality
$(document).ready(function(){
    $('.backToSearch').bind('click',function(){
        //window.location = window.location.pathname;
        history.back();
        
        return false;
    });
});

//adds selected class to header menu navigation and user navigation
$(document).ready(function(){
    $.each($('#topNav a'), function(i, e){
        var elem = $(e);
        
        if(elem.attr('href') == window.location.pathname && elem.attr('id') != 'productSearchLink')
            elem.addClass('selected');
    });
    
    $.each($('#productNav a'), function(i, e){
        var elem = $(e);
        if(elem.attr('href') == window.location.pathname)
            elem.addClass('selected');
    });
});


$(document).ready(function(){
    $('#searchLink').click(function(){
        search();
        return false;
    });
    
    $('#productSearchLink').click(function(){
        searchProducts();
        return false;
    });
    
    $('.searchInput').keyup(function(e){
        if(e.keyCode == 13){
            search();
        }
        
        return false;
    });
    
    function search()
    {
       var val = $('.searchInput').val();
       if(val != "" && val != search_text)
       {
            var url = $('#searchLink').attr('href') + '?search=' + val;
            window.location = url;
       }
    }
    
    function searchProducts()
    {
        var val = $('.searchInput').val();
        if(val != "" && val != search_text)
        {
            var url = $('#productSearchLink').attr('href') + '?act=search&term=' + val;
            window.location = url;
        }
    }
    
    $('.searchInput').keydown(function(e){
        if(e.keyCode == 13){
            return false;
        }
    });
});

//toggleLanguage and redirects to the specified url
$(document).ready(function(){
    $('a.toggleLanguage').click(function(){
        var url = $(this).attr('href');
        $.post('/CultureMethods.aspx?switch_culture=true', function(data){
            window.location.href = url;
        });
        
        return false;
    });
});
//culture change button
$(document).ready(function(){
    $('#cultureChange').click(function(){
        $.post('/CultureMethods.aspx?switch_culture=true', function(data){
            if(data == "English")
                window.location.href = '/Home.aspx';
            else
                window.location.href = '/Accueil.aspx'; 
        });
        
        return false;
    });
});


///sets up the breadcrumb trail.
$(document).ready(function(){
    var elem = $('#crumbs');
    
    if(elem.find('li').length > 0)
        return;

    var url = window.location.pathname.split('?')[0];
    var first = true;
    for(var i = 0; i< url.length; i++)
    {
        var c = url.charAt(i);
        if(c == '/' && !first)
        {
            var newUrl = url.substr(0, i);
            if(newUrl.indexOf('.aspx') == -1)
                newUrl = newUrl + '.aspx';
            
            var name = getName(newUrl);
            
            var newElem = getNewElement(newUrl, name);
            elem.append(newElem);
        }
        
        first = false;
    }
    
    var name = getName(url);
    var newElem = '<li><a class="noImage" href="' + url + '">' + name + '</a></li>';
    elem.append(newElem);
});

function getName(url)
{
    var name = url.substr(url.lastIndexOf('/')).replace('.aspx', '').replace('/', '').replace('-', ' ').replace('-', ' ').replace('-', ' ').replace('-', ' ').replace('-', ' ').replace('-', ' ').replace('-', ' ').replace('-', ' ');
    
    //fix kentico's mangling of french
    if(name.indexOf('l entreprise') != -1)
        name = name.replace('l entreprise', "l'entreprise")
       
    try
    {
        name = decodeURI(name);
        
    }
    catch(err){
        name = unescape(name);
    }
    
    return name;
}

function getNewElement(url, name)
{
    return '<li><a href="' + url + '">' + name + '</a></li>';
}

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
