// Set Homepage only for IE
if(jQuery.browser.msie == true){
	$('.link-make-homepage').click(function(){
		this.style.behavior='url(#default#homepage)';
		this.setHomePage('http://www.talktalk.co.uk');
		return false;
	});
}

var loc = String(document.location);
var def = $('#search-box-field #keyword').val();
if(!loc.match(".refresh=true")){ // don't place focus when page refreshed
	// bind focus and keydown events for cursor focus on search box
	var textfocus = 0;
	$('#search-box-field #keyword').bind('focus',function() { 
	  window.o=this;
	  if (o.setSelectionRange){ // DOM method
		o.setSelectionRange(0,0);
	  }else if (o.createTextRange){ // IE method
	   var r=o.createTextRange();
	   r.move('character',0);
	   r.select();
	  }
	  textfocus = 1;
	}).focus().bind('keypress',function(e){
		if($(this).val() == def || $(this).val() == 'Search the web here...'){
			if(e.keyCode != 39 && e.keyCode != 35){ // remove query only if not right arrow and end key
				$(this).attr('value','');
			}else {
				$(this).unbind('keypress');
			}
		}
	});
}else{
	var textfocus = 1;
}

$('#keyword').click(function(){ 
	$('#search-box-field #keyword').unbind('keypress').unbind('focus');
	if(($('#tt-search-types span.search-active a').attr('title') == this.value) && textfocus == 1){
		this.value = '';
		
	}
 });

var searchTitles = Array('Search the web here...','Compare product prices...','Search this site','Search video','Search local businesses','Search for TT customer help');

$('#tt-search-types a').click(function() {
	$(this).blur();
    var stypelong = $(this).attr('id');
    var stypeshort = stypelong.replace(/search-/i,'');
    $('#tt-search-types a').parent().removeClass('search-active');
    $(this).parent().addClass('search-active');
    $('#searchform input[name="type"]').val(stypeshort);

    if(stypeshort=='shopping'){
        $("#search-logo").attr({'class':"tt-hd1searchshopbg"});
    }else if(stypeshort=='business'){
        $("#search-logo").attr({'class':"tt-hd1searchlocalbg"});		
	}else if(stypeshort=='internal' || stypeshort=='video' || stypeshort=='help'){
        $("#search-logo").attr({'class':"tt-hd1searchinternalbg"});				
    }else{
        $("#search-logo").attr({'class':"tt-hd1searchgooglebg"});				
    }

    if($('#keyword').val() == ''){
        $('#keyword').val($(this).attr('title'));
    } else {
        var changeVar = false;
        for(i in searchTitles){
            if(searchTitles[i] == $('#keyword').val()) changeVar = true;
        }
        if(changeVar)
            $('#keyword').val($(this).attr('title'));
    }

    // remove the left dotted border on selected tabs
    $('#tt-search-types>span').not(':last').css({'border-right' : '1px dotted #B4B4B4'});
    var selectedtab = $(this).parent().prev();
	
    if($(selectedtab).is('span') || $('#tt-search-types a').index(this)==0) {
        $(this).parent().css({'border-right' : '0px'}).prev().css({'border-right' : '1px #fff solid'});
    }

    return false;
});

try{
	var absloc = location;
	var pathname = location.pathname;
	if(pathname.match(/search\/internal\.php$/i)){
		$('#search-internal').click();
	} else if(pathname.match(/search\/video-results\.html$/i)){
		$('#search-video').click();		
	} else if(pathname.match(/search\/local-results\.html$/i)){
		$('#search-business').click();		
	} else if((pathname.match(/^\/help\//i) && (!pathname.match(/^\/tiscali-product\//i)))){
		$('#search-help').click();		
	}else if(absloc.match(/broadband\.talktalk\.co\.uk/i)){
		$('#search-help').click();
	}else if(location.search.match(/section=help/i)){
		$('#search-help').click();
	}
}catch(e){}

