$(document).ready(function() 
{
     var root = 'http://' + window.location.hostname + '/';
     $("#phone").autocomplete( root + "suggest/", {
            width: 588,
            max: 100,
            highlight: false,
            scroll: true,
            scrollHeight: 405,
            formatItem: function(data, i, n, value) {
                    val = value;
                    val = val.replace( '#', '');
                    val = val.replace( '#', '');
                    return val;
            },
            formatResult: function(data, value) {
            	val = value.split("#")[1];
                    return val;
            }
    });
     
    $("#phone").result(function(event, data, formatted) {
    	$("#phone").css('color','white');
    	var href = $(data + ' div.hoverd').find('span.ref').html();
        window.location = href;
    });

    $("#phone").focus( function () {
        if( $(this).val() == 'Enter your phone make/model, eg. nokia n95' ) {
            $(this).val('').css('color','#4E4E50');
        }
    });
    $("#phone").blur( function() {
    	if( $(this).val() == '' || $(this).val() == ' ' ) {
            $(this).val('Enter your phone make/model, eg. nokia n95').css('color','#D2D2D2');
        }
    });

    
    // Slider
    $("ul.tabs a").click(function(){
    	$("ul.tabs a.selected").removeClass("selected");
    	$("#slider > div").hide();
    	var hash = $(this).attr('rel');
        if( hash ) {
        	$("#slider > div#" + hash).fadeIn("fast");
        	$(this).addClass("selected");
        }
    });
    
    // Matrix Hover
    
    $("ul.matrix > li > a").hover( function(){
    	$(this).parent().find("p").find("a").addClass( "hover" );
    }, function() {
    	$(this).parent().find("p").find("a").removeClass( "hover" );
    });
    
    
    // Error Images
    
    $("ul.matrix li img").error( function( ) { $(this).unbind("error").attr("src", root + "images/brands/error.gif"); });
    $("img.h1img").error( function( ) { $(this).unbind("error").attr("src", root + "images/brands/error.gif"); });
    $("img.phone").error( function( ) { $(this).unbind("error").attr("src", root + "images/phones/error.gif"); });
    $("#submit-comment").click(function(){
    	$("#submit-comment").css('display', 'none');
    	$("#loading2").css('display', 'block');
    });
    
});

function rotateSlider( tab ) {
	$("ul.tabs a.selected").removeClass("selected");
	$("#slider > div").hide();
    $("#slider > div#tab" + tab).fadeIn("fast");
	$("ul.tabs a[rel='tab" + tab + "']").addClass("selected");
	tab = ( tab == 3 ) ? 1 : tab + 1;
	setTimeout( "rotateSlider(" + tab + ");", 5000 );
}

//Slide Rotation
setTimeout( "rotateSlider(2);", 5000 );
