/*
* jquery for Vision Personal Training site
*/

$(document).ready(function () {

    $("#ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_tbSearch").keypress(function (evt) {
        if (evt.keyCode == 13) {
            $("#ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_imgbtnSearch").click();
        }
    });

    // convert enter to tab in forms
    $("input").not($(":button")).keypress(function (evt) {
        if (evt.keyCode == 13) {
            iname = $(this).val();
            if (iname !== 'Submit') {
                var fields = $(this).parents('form:eq(0),body').find('button, input, textarea, select');
                var index = fields.index(this);
                if (index > -1 && (index + 1) < fields.length) {
                    fields.eq(index + 1).focus();
                }
                return false;
            }
        }
    });

    $("#loginPass").keypress(function (evt) {
        if (evt.keyCode == 13) {
            $("#loginSubmit").click();
        }
    });

    $("#loginSubmit").keypress(function (evt) {
        if (evt.keyCode == 13) {
            $("#loginSubmit").click();
        }
    });


    $(".clubvision select").select_skin();

    var tmrSet, tmrSetB;

    $('.subMenuParent').hoverIntent(function () {
        $('.subMenuContainer').hide();
        $('.subMenuParent a').removeClass('over');
        //show current layer, if rolling over then stop hiding current layer by clearTimeout
        clearTimeout(tmrSet);
        $(this).find('div.subMenuContainer').show();
        $(this).find('a').addClass('over');
    }, function () {
        var sm_container = $(this).find('div.subMenuContainer');
        //var sm_parent = $(this).find('.subMenuParent a');
        var sm_parent = $(this).parent().find('a');
        tmrSet = setTimeout(function () {
            //alert("mouseout"); 
            $(sm_container).hide();
            $(sm_parent).removeClass('over');
        }, 100);
    });


    $("#vptLogin").hoverIntent(function () {
        $("#vptLogin").animate({ "height": "141px" }, "fast");
    }
    , function () {
        $("#vptLogin").animate({ "height": "37px" }, "fast");
    });

    $("#weighInButton").click(function () {
        $("#ContentPlaceHolderDefault_ContentPlaceHolderMain_ProfileTab_2_weighInPopup").show();
    });

    $("#weighInCancel").click(function () {
        $("#ContentPlaceHolderDefault_ContentPlaceHolderMain_ProfileTab_2_weighInPopup").hide();
    });

    $("#ContentPlaceHolderDefault_ContentPlaceHolderMain_ProfileTab_2_bdpWhen_TextBox").attr("disabled", "disabled");

    $("#diary-entries").append($("#macro-header").clone());
    $("#diary-entries").append($("#macro-total").clone());
    $("#diary-entries").append($("#macro-goal").clone());
    $("#diary-entries").append($("#macro-diff").clone());

    $("#menu-items").append($("#macro-header").clone());
    $("#menu-items").append($("#macro-total").clone());
    $("#menu-items").append($("#macro-goal").clone());
    $("#menu-items").append($("#macro-diff").clone());

    $(".item-drag").draggable({ containment: "#foodDiaryWeek", scroll: false, cursorAt: { cursor: "move", top: 10, left: 10 }, snap: ".foodDairyWeekCell", snapMode: "inner", revert: "invalid", helper: "clone" });
    $(".meal-drag").draggable({ containment: "#foodDiaryWeek", scroll: false, cursorAt: { cursor: "move", top: 10, left: 10 }, snap: ".foodDairyWeekCell", snapMode: "inner", revert: "invalid", helper: "clone" });

    $(".foodDairyWeekCell").droppable({ drop: drophandle });


    function drophandle(event, ui) {
        if (ui.draggable.parent().data("mealtimeid") == $(event.target).data("mealtimeid") && ui.draggable.parent().data("day") == $(event.target).data("day")) {
        }
        else {
            if (ui.draggable.data("type") == 'meal') {
                ui.draggable.removeAttr('style');
                element = ui.draggable.clone(false);
                $(event.target).append(element);
                element.attr("data-day", $(event.target).attr("data-day"));
                element.attr("data-mealtimeid", $(event.target).attr("data-mealtimeid"));
                ui.draggable.attr("style", "position: relative");
                element.attr("style", "position: relative");

                CopyMealToDateMealTime(ui.draggable.data("id"), ui.draggable.data("day"), $(event.target).data("day"), $(event.target).data("mealtimeid"));

                element.find("p.item-drag").attr("data-day", $(event.target).attr("data-day"));
                element.find("p.item-drag").attr("data-mealtimeid", $(event.target).attr("data-mealtimeid"));
                element.find("p.item-drag").draggable({ containment: "#foodDiaryWeek", scroll: false, cursorAt: { cursor: "move", top: 10, left: 10 }, snap: ".foodDairyWeekCell", snapMode: "inner", revert: "invalid", helper: "clone" });
                element.draggable({ containment: "#foodDiaryWeek", scroll: false, cursorAt: { cursor: "move", top: 10, left: 10 }, snap: ".foodDairyWeekCell", snapMode: "inner", revert: "invalid", helper: "clone" });

                if (ui.draggable.parent().data("day") == $(event.target).data("day")) {
                    ui.draggable.remove();
                }
            }
            else {
                ui.draggable.removeAttr('style');
                element = ui.draggable.clone(false);
                $(event.target).append(element);
                element.attr("data-day", $(event.target).attr("data-day"));
                element.attr("data-mealtimeid", $(event.target).attr("data-mealtimeid"));
                ui.draggable.attr("style", "position: relative");
                element.attr("style", "position: relative");

                CopyItemToDateMealTime(ui.draggable.data("id"), $(event.target).data("day"), $(event.target).data("mealtimeid"));

                element.draggable({ containment: "#foodDiaryWeek", scroll: false, cursorAt: { cursor: "move", top: 10, left: 10 }, snap: ".foodDairyWeekCell", snapMode: "inner", revert: "invalid", helper: "clone" });
            }
        }
    }

    //    //tabs
    $('#teamNav li a').click(function () {
        //reset all layers first
        $('#teamNav li a').removeClass('on');
        $('.teamContent').hide();
        //set on states
        $(this).addClass('on');
        var hrefValue = $(this).attr('href');
        var tmrSet = setTimeout(function () {
            $(hrefValue).fadeIn();
        }, 50);
        return false;
    });


    if ($(".cDate").length > 0) {
        $('.cDate').click(function () {
            $('.chooseDate').toggle();
            return false;
        });
    }


    //only set up if overlay exists on page

    //alert($(window).height());	
    //alert($(document).height());	
    //var tmrSet = setTimeout(function(){ 
    //alert($(window).scrollTop());
    //scroll_top_amount = $(window).scrollTop();
    //add this amount to the margin top of lightbox so it is down the page where you are browsing.
    //}, 1000);


    $(".contactOverlay").css("height", $(document).height());
    $(".heightTable").css("height", $(window).height());
    //$(".contactBox").css("margin-top", $(window).height());

    $(".cConsultationOpen").click(function () {
        var scroll_top_amount = $(window).scrollTop();
        //alert($(".contactBox").offset().top);
        //alert($(window).scrollTop());
        $(".contactBox").css("margin-top", ((($(window).height() - 460) / 2) + $(window).scrollTop() + 0) + "px");
        $("#cConsultation").fadeIn();
        return false;
    });
    $(".cConsultationClose").click(function () {
        $("#cConsultation").fadeOut();
        return false;
    });

    $(".cEnquireOpen").click(function () {
        $(".contactBox").css("margin-top", ((($(window).height() - 464) / 2) + $(window).scrollTop() + 0) + "px");
        $("#cEnquire").fadeIn();
        return false;
    });
    $(".cEnquireClose").click(function () {
        $("#cEnquire").fadeOut();
        return false;
    });

    $(".cFindOpen").click(function () {
        $(".contactBox").css("margin-top", ((($(window).height() - 564) / 2) + $(window).scrollTop() + 0) + "px");
        $("#cFind").fadeIn();
        return false;
    });
    $(".cFindClose").click(function () {
        $("#cFind").fadeOut();
        return false;
    });
});
$(window).bind("resize", function () {
    $(".contactOverlay").css("height", $(document).height());
    $(".heightTable").css("height", $(window).height());
});

function ShowVideo(id, title, description, linkURL, linkText, mealsLink) {
	
    var strhtml = '<object id="myExperienceSSS" class="BrightcoveExperience">';
	strhtml += '<param name="bgcolor" value="#FFFFFF" />';
	strhtml += '<param name="width" value="480" />';
	strhtml += '<param name="height" value="270" />';
	strhtml += '<param name="playerID" value="1081504048001" />';
	strhtml += '<param name="playerKey" value="AQ~~,AAAA6xwg44E~,ysHMNUKUQO51nUTSl4HmEFQjMsQI8Sli" />';
	strhtml += '<param name="isVid" value="true" />';
	strhtml += '<param name="isUI" value="true" />';
	strhtml += '<param name="dynamicStreaming" value="true" />';
	strhtml += '<param name="wmode" value="transparent" />';
	strhtml += '<param name="@videoPlayer" value="' + id + '" />';
	strhtml += '</object>';


	// Add the embed object to your container
	document.getElementById("player").innerHTML = strhtml;
	
	// Add a sibling for the embed object
	//document.getElementById("myExperienceSSS").innerHTML += "<div></div>";

    document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lbFeaturedVideoTitle").innerHTML = title;
    document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lbFeaturedVideoDesc").innerHTML = description;
    document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lnkRelated").innerHTML = linkText;
    document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lnkRelated").href = linkURL;
    if(mealsLink == 0)
    {
        document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lnkFoodDiary").style.display = 'none';
    }
    else
    {
        document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lnkFoodDiary").style.display = 'block';
        document.getElementById("ContentPlaceHolderDefault_ContentPlaceHolderMain_VisionTV_2_lnkFoodDiary").href = '/club-vision/menus/?tab=view_meal&mealId=' + mealsLink;
    }
	
	// Fire off the instantiators (to ensure the object is ready in the DOM)
	initPlayer();

}

function initPlayer() {
	brightcove.createExperiences();
}


/*
Written by Steve Tucker, 2006, http://www.stevetucker.co.uk
Full documentation can be found at http://www.stevetucker.co.uk/page-innerxhtml.php
Released under the Creative Commons Attribution-Share Alike 3.0  License, http://creativecommons.org/licenses/by-sa/3.0/

Change Log
----------
15/10/2006	v0.3	innerXHTML official release.
21/03/2007	v0.4	1. Third argument $appendage added (Steve Tucker & Stef Dawson, www.stefdawson.com)
2. $source argument accepts string ID (Stef Dawson)
3. IE6 'on' functions work (Stef Dawson & Steve Tucker)
*/
innerXHTML = function ($source, $string, $appendage) {
    // (v0.4) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk
    if (typeof ($source) == 'string') $source = document.getElementById($source);
    if (!($source.nodeType == 1)) return false;
    var $children = $source.childNodes;
    var $xhtml = '';
    if (!$string) {
        for (var $i = 0; $i < $children.length; $i++) {
            if ($children[$i].nodeType == 3) {
                var $text_content = $children[$i].nodeValue;
                $text_content = $text_content.replace(/</g, '&lt;');
                $text_content = $text_content.replace(/>/g, '&gt;');
                $xhtml += $text_content;
            }
            else if ($children[$i].nodeType == 8) {
                $xhtml += '<!--' + $children[$i].nodeValue + '-->';
            }
            else {
                $xhtml += '<' + $children[$i].nodeName.toLowerCase();
                var $attributes = $children[$i].attributes;
                for (var $j = 0; $j < $attributes.length; $j++) {
                    var $attName = $attributes[$j].nodeName.toLowerCase();
                    var $attValue = $attributes[$j].nodeValue;
                    if ($attName == 'style' && $children[$i].style.cssText) {
                        $xhtml += ' style="' + $children[$i].style.cssText.toLowerCase() + '"';
                    }
                    else if ($attValue && $attName != 'contenteditable') {
                        $xhtml += ' ' + $attName + '="' + $attValue + '"';
                    }
                }
                $xhtml += '>' + innerXHTML($children[$i]);
                $xhtml += '</' + $children[$i].nodeName.toLowerCase() + '>';
            }
        }
    }
    else {
        if (!$appendage) {
            while ($children.length > 0) {
                $source.removeChild($children[0]);
            }
            $appendage = false;
        }
        $xhtml = $string;
        while ($string) {
            var $returned = translateXHTML($string);
            var $elements = $returned[0];
            $string = $returned[1];
            if ($elements) {
                if (typeof ($appendage) == 'string') $appendage = document.getElementById($appendage);
                if (!($appendage.nodeType == 1)) $source.appendChild($elements);
                else $source.insertBefore($elements, $appendage);
            }
        }
    }
    return $xhtml;
}
function translateXHTML($string) {
    var $match = /^<\/[a-z0-9]{1,}>/i.test($string);
    if ($match) {
        var $return = Array;
        $return[0] = false;
        $return[1] = $string.replace(/^<\/[a-z0-9]{1,}>/i, '');
        return $return;
    }
    $match = /^<[a-z]{1,}/i.test($string);
    if ($match) {
        $string = $string.replace(/^</, '');
        var $element = $string.match(/[a-z0-9]{1,}/i);
        if ($element) {
            var $new_element = document.createElement($element[0]);
            $string = $string.replace(/[a-z0-9]{1,}/i, '');
            var $attribute = true;
            while ($attribute) {
                $string = $string.replace(/^\s{1,}/, '');
                $attribute = $string.match(/^[a-z1-9_-]{1,}="[^"]{0,}"/i);
                if ($attribute) {
                    $attribute = $attribute[0];
                    $string = $string.replace(/^[a-z1-9_-]{1,}="[^"]{0,}"/i, '');
                    var $attName = $attribute.match(/^[a-z1-9_-]{1,}/i);
                    $attribute = $attribute.replace(/^[a-z1-9_-]{1,}="/i, '');
                    $attribute = $attribute.replace(/;{0,1}"$/, '');
                    if ($attribute) {
                        var $attValue = $attribute;
                        if ($attName == 'value') $new_element.value = $attValue;
                        else if ($attName == 'class') $new_element.className = $attValue;
                        else if ($attName == 'style') {
                            var $style = $attValue.split(';');
                            for (var $i = 0; $i < $style.length; $i++) {
                                var $this_style = $style[$i].split(':');
                                $this_style[0] = $this_style[0].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/, '');
                                $this_style[1] = $this_style[1].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/, '');
                                if (/-{1,}/g.test($this_style[0])) {
                                    var $this_style_words = $this_style[0].split(/-/g);
                                    $this_style[0] = '';
                                    for (var $j = 0; $j < $this_style_words.length; $j++) {
                                        if ($j == 0) {
                                            $this_style[0] = $this_style_words[0];
                                            continue;
                                        }
                                        var $first_letter = $this_style_words[$j].toUpperCase().match(/^[a-z]{1,1}/i);
                                        $this_style[0] += $first_letter + $this_style_words[$j].replace(/^[a-z]{1,1}/, '');
                                    }
                                }
                                $new_element.style[$this_style[0]] = $this_style[1];
                            }
                        }
                        else if (/^on/.test($attName)) $new_element[$attName] = function () { eval($attValue) };
                        else $new_element.setAttribute($attName, $attValue);
                    }
                    else $attribute = true;
                }
            }
            $match = /^>/.test($string);
            if ($match) {
                $string = $string.replace(/^>/, '');
                var $child = true;
                while ($child) {
                    var $returned = translateXHTML($string, false);
                    $child = $returned[0];
                    if ($child) $new_element.appendChild($child);
                    $string = $returned[1];
                }
            }
            $string = $string.replace(/^\/>/, '');
        }
    }
    $match = /^[^<>]{1,}/i.test($string);
    if ($match && !$new_element) {
        var $text_content = $string.match(/^[^<>]{1,}/i)[0];
        $text_content = $text_content.replace(/&lt;/g, '<');
        $text_content = $text_content.replace(/&gt;/g, '>');
        var $new_element = document.createTextNode($text_content);
        $string = $string.replace(/^[^<>]{1,}/i, '');
    }
    $match = /^<!--[^<>]{1,}-->/i.test($string);
    if ($match && !$new_element) {
        if (document.createComment) {
            $string = $string.replace(/^<!--/i, '');
            var $text_content = $string.match(/^[^<>]{0,}-->{1,}/i);
            $text_content = $text_content[0].replace(/-->{1,1}$/, '');
            var $new_element = document.createComment($text_content);
            $string = $string.replace(/^[^<>]{1,}-->/i, '');
        }
        else $string = $string.replace(/^<!--[^<>]{1,}-->/i, '');
    }
    var $return = Array;
    $return[0] = $new_element;
    $return[1] = $string;
    return $return;
}
