﻿$(document).ready(function () {

    // buy online hover
    $("ul#buyOnlineLinks img").hover(
        function() {
            var originalImage = $(this).attr("src");
            $(this).attr("src", originalImage.replace(".jpg", "_over.jpg"));
        },
        function() {
            var overImage = $(this).attr("src");
            $(this).attr("src", overImage.replace("_over", ""));
        }
    );
	if(undefined != perm_blonde_ids)
	{
	    var shades = { 
		    "perm" : {
			    "blond" : { "ids" : perm_blonde_ids, "names" : perm_blonde_names },
			    "red" : { "ids" : perm_red_ids, "names" : perm_red_names },
			    "black" : { "ids" : perm_black_ids, "names" : perm_black_names }
		    },
		    "tf" : {
			    "blond" : { "ids" : tf_blonde_ids, "names" : tf_blonde_names },
			    "red" : { "ids" : tf_red_ids, "names" : tf_red_names },
			    "black" : { "ids" : tf_black_ids, "names" : tf_black_names }
		    },
		    "eight" : {
			    "blond" : { "ids" : eight_blonde_ids, "names" : eight_blonde_names },
			    "red" : { "ids" : eight_red_ids, "names" : eight_red_names },
			    "black" : { "ids" : eight_black_ids, "names" : eight_black_names }
		    }
	    };
	}
	
	$("div#shades a").attr("href", "#");

    $("div#shades a").click(function() {
        if ($(this).attr("class") != "on") {
            $("div#shades a").attr("class", "off");
            $(this).attr("class", "on");
            $("div#shades h3").html($(this).html());
            var id = $(this).attr("id").replace("ctl00_ctl00_body_body_", "").replace("_link", "");
            $("div#shades h3").attr("class", id);
			colours = ["blond", "red", "black"];
			for (var i=0; i<colours.length; i++){
				var colour = colours[i];
				$("div." + colour + " table tbody tr[id]").remove();
				for (var iter=0; iter<shades[id][colour]["ids"].length; iter++){
					$("div." + colour + " table tbody").append(formShade(shades[id][colour]["ids"][iter], shades[id][colour]["names"][iter]));
				}
			}
		}
		return false;
	});
});

function formShade(shade_id, shade_name) {
	var height = "15";
	if (in_array(shade_id, new_shades)) height = "20";
	var cell1 = "<td valign=\"top\" width=\"54\"><img src=\"/niceneasy/images/shades/shade_nne_" + shade_id + ".gif\" height=\"" + height + "\" /></td>";
	var cell2 = "<td align=\"center\" width=\"54\">" + shade_id + "</td>";
	var cell3 = "<td width=\"231\">" + shade_name + "</td>";
	return "<tr id=\"shade_" + shade_id + "\">" + cell1 + cell2 + cell3 + "</tr>";
}

function in_array(needle, haystack, argStrict) {
    var found = false, key, strict = !!argStrict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? document.getElementsByTagName('object')[0] : document[movieName];
}



$(document).ready(function () {

    //remove default video images on page load
    $('#howToVideoWrapper').css("background", "none");
    $('#videoContainer').css("background", "none");

    //click handler for youtube video links
    $('.chooseAQuestionContainer a').click(function (e) {

        e.preventDefault();
        $('.chooseAQuestionContainer a').removeClass("selected");
        $(this).addClass("selected");
        var rel = $(this).attr('rel');
        window.location.hash = '#' + rel;
        $('.btnFacebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location) + '&t=How%20To%20Videos');
        var theVideo = $(this).attr('href').split('=')[1];
        var theTitle = $(this).find('span').html();
        loadswf(theVideo, theTitle);
    });

    if ($('#TVAdvert').length > 0) $('.btnFacebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location) + '&t=TV%20Advert');
    else if ($('#howToVideos').length > 0) $('.btnFacebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location) + '&t=How%20To%20Videos');

    var hash = window.location.hash.substr(1);
    //if (hash.length > 0) $('.chooseAQuestionContainer a').eq(hash).click();
    if (hash.length > 0) $('.chooseAQuestionContainer a[rel=' + hash + ']').click();
    else $('.chooseAQuestionContainer a').eq(0).addClass("selected");
});

//function to switch youtube videos

function loadswf(vidid, vidtitle) {
     //alert(vidid);
     var flashvars = { videoID: vidid, videoTitle: vidtitle };
     var params = { allowFullScreen: "false", menu: "false", wmode: "transparent" };
     var attributes = null;
     swfobject.embedSWF("/swf/howto.swf", "howtovid", "508", "327", "9", "/swf/expressInstall.swf", flashvars, params, attributes, null);
}
