$(document).ready(function () {

    // Star the glowing
    headerSearchBoxGlowForward();
    footerSearchBoxGlowForward();
    fopSearchBoxGlowForward();
    colSearchBoxGlowForward();

    bindPlaceholders();

    if (navigator.geolocation) {

        //$('.col_fop').append('<a href="#" class="currentlocation">use my current location</a>');
    }


    /* Spec Interaction Stuff */

    $('#activeArea').hover(
					function () {
					    // In
					    $('#specs').fadeIn('fast', function () {

					        // Need to fix specs PNG issue in IE 6
					        $('#specs').supersleight();
					    });
					},
					function () {
					    // Out
					    $('#specs').fadeOut('slow');

					});

    $('#activeArea').mousemove(function (e) {

        posY = e.pageY - ($('#specs').height() / 2);
        posX = e.pageX - ($('#specs').width() / 2);

        $('#specs').css('top', posY + 'px');
        $('#specs').css('left', posX + 'px');
    });
  
    /* Swap Interaction Stuff */

    $('#seeTheDifference').find('a').bind('click', function () {

        $('#selected_box').remove();

        // Item
        lensItem = $(this).attr('alt');

        // Remove old stuff
        $('#selected_box').remove();
        var prevSelected = $('.arrowMenu .selected');
        $(prevSelected).removeClass('selected');
        $(prevSelected).css('filter', 'none');

        // Update menu selection
        $(this).addClass('selected');
        $(this).parent().prepend('<div id="selected_box">');


        // IE6
        if (!window.XMLHttpRequest) {
            $('#selected_box').supersleight();
            $('.selected').supersleight();
        }

        $('#flash').fadeIn(100, function () {
            // Swap images
            $('#seeTheDifference').css('background-image', "url('images/splash/" + lensItem + ".jpg')");
            $('#header').css('background-image', "url('images/headers/" + lensItem + ".jpg')");
            $('#specs').css('background-image', "url('images/specs/" + lensItem + ".png')");
            $('.desctest').hide();
            $('.desctest[rel|=' + lensItem + ']').show();

            $('#flash').fadeOut(1000);


        });

    });


	$('.recomended_op').live('mouseenter',function(){
			$(this).prepend('<div id="rec-bub"><span><h2>Why a Crizal Optician?</h2><p>Crizal recommended opticians have been awarded this status because of their expertise and experience in dispensing Crizal lenses.  They can offer you a completely personalised service and will recommend only the very best eye care solution to suit the needs of every individual.</p></span></div>');
			$('#rec-bub').stop(true,true).fadeIn(300);
		
		});
		
		$('.recomended_op').live('mouseout',function(){
			$('#rec-bub').stop(true,true).fadeOut(300,function(){
				$('#rec-bub').remove();
			});
		});

    // ---- TABS ----- //

    // Hide All Tabs Onload
    $('.tabView').hide();
    // Setup tabs
    var tabsArray = $('.tabView');   
    // Show first tab in array

    if (window.location.hash.substr(1) != "") {

        hashTab = window.location.hash.substr(1);
        $('.tabView[rel|=' + hashTab + ']').show();
        $('.arrowMenu li a[alt|=' + hashTab + ']').addClass('selected');
        $('.arrowMenu li a[alt|=' + hashTab + ']').parent().prepend('<div id="selected_box">');

    } else {
        $(tabsArray[0]).show();
        $('.arrowMenu li a[alt|=' + $(tabsArray[0]).attr('rel') + ']').addClass('selected');
        $('.arrowMenu li a[alt|=' + $(tabsArray[0]).attr('rel') + ']').parent().prepend('<div id="selected_box">');

    }



    $('#tabs').find('a').bind('click', function () 
    {
        // Remove old stuff
        $('#selected_box').remove();
        $('#selected_box').remove();

        var prevSelected = $('.arrowMenu .selected');
        $(prevSelected).removeClass('selected');
        $(prevSelected).css('filter', 'none');

        // Update menu selection
        $(this).addClass('selected');
        $(this).parent().prepend('<div id="selected_box">');
        $(this).supersleight();
        $('#selected_box').supersleight();




        newTab = $(this).attr('alt');

        // Hide prev tab
        $('.tabView').hide();
        $('.tabView[rel|=' + newTab + ']').show();


    });


    $('.currentlocation').live('click', function (e) {

        $('.currentlocation').fadeOut('fast');
        $('.col_fop .search').fadeOut('fast');
        $('#error').html("Locating you, one moment please...");
        $('#error').fadeIn('fast');



        navigator.geolocation.getCurrentPosition(function (position) {

            alert("FOUND");
        }, function (error) {
            alert("ERROR");

        });

    });


    $('.search_button').live('click', function (e) {

        searchID = $(this).attr('id');
        searchValue = ($('#' + searchID + '_value').val());

        if (validatePostcode(searchValue) == true) {
            window.location = "rfwd.html#" + searchValue.split(' ').join(''); ;
        }

        return false;

    });

    $('.search_box input').live('keydown', function (e) {

        if (e.keyCode == 13) {
            if (validatePostcode($(this).val()) == true) {
                window.location = "rfwd.html#" + $(this).val().split(' ').join(''); ;
                return false;
            }
        }

    });


});
 
function headerSearchBoxGlowForward(){
	$('#header .search_glow').animate({
		backgroundPosition:['+=600','linear']
	},4000,function(){
		headerSearchBoxGlowForward();
	});
}

function footerSearchBoxGlowForward(){
	$('#footer .search_glow').animate({
		backgroundPosition:['+=600','linear']
	},4000,function(){
		footerSearchBoxGlowForward();
	});
}

function fopSearchBoxGlowForward(){
	$('.col_fop .search_glow').animate({
		backgroundPosition:['+=600','linear']
	},4000,function(){
		fopSearchBoxGlowForward();
	});
}


function colSearchBoxGlowForward(){
	$('.col_480_l .search_glow').animate({
		backgroundPosition:['+=600','linear']
	},4000,function(){
		colSearchBoxGlowForward();
	});
}

function bindPlaceholders(){

	// Put place holder text in inputs
	inputs = $('input');
	i = 0;
	
	while (i < inputs.length){
		$(inputs[i]).val($(inputs[i]).attr('placeholder'));
		i++;
	}
	
	
	$('input').bind('focusin',function(){
		if($(this).val() == $(this).attr('placeholder') ){
			$(this).val("");
			$(this).css('color','#000');
			$(this).css('text-transform','uppercase');
		
			$('.search_hint').fadeIn('fast');
		}
	});
	
	$('input').bind('focusout',function(){
		if($(this).val() == ""){
			$(this).val($(this).attr('placeholder'));
			$(this).css('color','#999');
			$(this).css('text-transform','none');
			
			$('#search_hint').fadeOut('fast');
		} 
	});

}

function bindNumbers(){

	$('.search_number').bind('click',function(){
	
		$('#phoneoverlay').fadeIn('fast');
		$('#phoneoverlay span').html($(this).html());
		
		return false;
		
	});
	
	$('#phoneoverlay').bind('click', function(){
		$(this).fadeOut('fast');
	});
	
}

function validatePostcode(pc){
	
		pc = pc.split(' ').join('');
		
		var pcodeRegxp = /^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]{2})$/;
		
		if(pcodeRegxp.test(pc) != true){
			
			
			displayErrorMessage("Please enter a valid Post Code");
			
			return false;
		} else {
			return true;	
		}

}

function displayErrorMessage(message){

	$('#error').html(message);
	$('#error').fadeIn('fast');
	
	setTimeout(function(){
		$('#error').fadeOut('slow');
	}, 2000);

}



