//<![CDATA[
$(document).ready(function() {
	//$("li.menu_teacher").hover(function(){alert("hello");});
    $("#message_x").click(function() {
        $("#message").slideUp();
        $.get("_ajax_close_message.php", {page: "index.php"});
    });
    $("#video_share_button").click(function() {
        if ($("#video_share_box").is(":hidden")) {
            $("#video_save_box").hide(0);
            $("#video_share_box").slideDown("fast");
        } else {
            $("#video_share_box").slideUp("fast");
            $("#emailtofriend").slideUp(0);
            $("#emailconf").hide(0);
        }
    });
    $("#video_share_x").click(function() {
        $("#video_share_box").slideUp("fast");
        $("#emailtofriend").slideUp(0);
        $("#emailconf").hide(0);
    });
    $("#video_save_button").click(function() {
        if ($("#video_save_box").is(":hidden")) {
            $("#video_share_box").hide(0);
            $("#video_save_box").slideDown("fast");
            //document.getElementById("video_save_comments").focus();
        } else {
            $("#video_save_box").slideUp("fast");
        }
    });
    $("#video_save_submit").click(function() {
        $.get("_ajax_save_favorite.php",{v:$("#video_save_id").val(),m:$("#video_save_comments").val()});
        $("#video_save_box").hide(0);
        $("#video_save_button").hide(0);
    });
    $("#video_save_x").click(function() {
        $("#video_save_box").slideUp("fast");
    });
    $("#comm_addpractice_button").click(function() {
        if ($("#comm_addpractice_box").is(":hidden")) {
            $("#comm_addpractice_box").slideDown("fast");
        } else {
            $("#comm_addpractice_box").slideUp("fast");
        }
    });
    $("#comm_addpractice_submit").click(function() {
        var year = $("#y").val();
        var month = $("#m").val();
        var day = parseInt($("#d").val(), 10);
        var time = $("input[name='time']:checked").val();
        var hours = parseFloat((time / 60).toFixed(1));
        $.get("_ajax_save_practice.php",{y:year,m:month,d:day,t:time});
        if (year == tY && month == tM) {
            var boxval = parseFloat($("#d"+day)[0].innerHTML);
            if (isNaN(boxval)) boxval = 0;
            var newhours = (boxval + hours).toFixed(1);
            $("#d"+day)[0].innerHTML = newhours;
            $("#x"+day)[0].style.visibility = "visible";
        }
        var today = new Date().getTime();
        var changeday = new Date().setFullYear(year,month-1,day);
        var diff = Math.abs(today - changeday);
        if (diff < (1000*60*60*24*7)) fnBlueBar(hours);
        $("#comm_addpractice_box").hide("fast");
    });
    $("#comm_addpractice_x").click(function() {
        $("#comm_addpractice_box").slideUp("fast");
    });
    $("input[name='cancelaction']:nth(0)").click(function() {
        $("#cancelreason").slideUp("fast");
    });
    $("input[name='cancelaction']:nth(1)").click(function() {
        $("#cancelreason").slideDown("fast");
    });
});

function fnCommDelHrs(year, month, day) {
	var oldval = parseFloat($("#d"+day)[0].innerHTML);
    $("#d"+day)[0].innerHTML = "";
    $("#x"+day)[0].style.visibility = "hidden";
    $.get("_ajax_delete_practicelog.php",{y:year,m:month,d:day});
    var today = new Date().getTime();
    var changeday = new Date().setFullYear(year,month-1,day);
    var diff = Math.abs(today - changeday);
    if (diff < (1000*60*60*24*7)) fnBlueBar(oldval * -1);
}

function fnBlueBar(hours) {
    var hpw = $("#comm_hoursperweek_num")[0];
    var bar = $("#comm_hoursperweek_bar")[0];
    var flower = $("#flower")[0];
    bar_hours += hours;
    if (bar_hours <= 0) {
        hpw.innerHTML = "";
        bar.style.width = "0";
        flower.src = "images/graylotus.jpg";
    } else if (bar_hours < 3.5) {
        var barwidth = Math.round(101 * bar_hours);
        hpw.style.left = (barwidth + 15) + "px";
        hpw.innerHTML = bar_hours.toFixed(1);
        bar.style.width = barwidth + "px";
        flower.src = (bar_hours < 1.75) ? "images/graylotus.jpg" : "images/midglowlotus.jpg";
    } else {
        hpw.innerHTML = "";
        bar.style.width = "355px";
        flower.src = "images/fullglowlotus.jpg";
    }
}

function fnFBSettings(choice) {
	switch (choice) {
	case "fb_teachers":
	    $("#fb_teachers").slideToggle("fast");
	    break;
    case "all_teachers":
        $("input[name='teacher[]']").attr("checked","");
        break;
    case "teachers":
        $("input[name='all_teachers']:nth(1)").attr("checked","checked");
        break;
	case "fb_durations":
	    $("#fb_durations").slideToggle("fast");
	    break;
    case "all_durations":
        $("input[name='video_len[]']").attr("checked","");
        break;
    case "durations":
        $("input[name='all_durations']:nth(1)").attr("checked","checked");
        break;
	case "fb_styles":
	    $("#fb_styles").slideToggle("fast");
	    break;
    case "all_styles":
        $("input[name='style[]']").attr("checked","");
        break;
    case "styles":
        $("input[name='all_styles']:nth(1)").attr("checked","checked");
        break;
	case "fb_levels":
	    $("#fb_levels").slideToggle("fast");
	    break;
    case "all_levels":
        $("input[name='level[]']").attr("checked","");
        break;
    case "levels":
        $("input[name='all_levels']:nth(1)").attr("checked","checked");
        break;
	}
}

function fnClearDefault(field) {
	if (field.value == field.defaultValue) field.value = "";
}

function fnRestoreDefault(field) {
	if (field.value == "") field.value = field.defaultValue;
}

function fnPass2Text(field) {
	if (field.value == "") {
		field.style.display = "none";
		var inp2 = field.parentNode.getElementsByTagName("input")[0];
		inp2.style.display = "block";
	}
}

function fnText2Pass(field) {
	field.style.display = "none";
	var inp2 = field.parentNode.getElementsByTagName("input")[1];
	inp2.style.display = "block";
	inp2.focus();
}

function fnOpenPopupMessage(id) {
    document.getElementById("iduservideoaction").value = id;
    document.getElementById("overlay").style.visibility = "visible";
    self.scrollTo(0, 0);
    document.getElementById("popup_m500").style.display = "block";
}

function fnClosePopupMessage() {
    document.getElementById("popup_m500").style.display = "none";
    document.getElementById("overlay").style.visibility = "hidden";
}

function fnEmailToFriend(action) {
    if (action == 1) {
    	$("#emailconf").hide(0);
        $("#emailtofriend").slideDown("fast");
    } else if (action == 2) {
        var email = $("input[name='emailaddress']").val();
        var idvideo = $("input[name='emailurl']").val();
        var regex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,})$/;
        if (email.search(regex) == -1) {
            alert("Please enter a valid email address.");
            $("#emailaddress").focus();
            return;
        }
        $.get("_ajax_email_to_friend.php",{e:email,u:idvideo});
	    $("#emailtofriend").hide(0);
	    $("input[name='emailaddress']").val("");
	    $("#emailconf").show(0);
    } else {
        $("#emailtofriend").slideUp(0);
    }
}
//Wc = weekly calender
function fnEmailToFriendWc(action, id) {
    if (action == 1) {
    	$("#emailconf"+id).hide(0);
        $("#emailtofriend"+id).slideDown("fast");
    } else if (action == 2) {
        var email = $("input[name='emailaddress"+id+"']").val();
        var idvideo = $("input[name='emailurl"+id+"']").val();
        var regex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,})$/;
        if (email.search(regex) == -1) {
            alert("Please enter a valid email address.");
            $("#emailaddress"+id).focus();
            return;
        }
        $.get("_ajax_email_to_friend_wc.php",{e:email,u:idvideo});
	    $("#emailtofriend"+id).hide(0);
	    $("input[name='emailaddress"+id+"']").val("");
	    $("#emailconf"+id).show(0);
    } else {
        $("#emailtofriend"+id).slideUp(0);
    }
}

/*function fnFacebookShareLink(URL) {
    window.open("http://www.facebook.com/share.php?u=http://www.yogaglo.com" + URL,"FacebookShare","resizable=1,toolbar=0,status=0,width=800,height=450");
}*/

function fnFacebookShareLink(URL) {
    window.open(URL,"FacebookShare","resizable=1,toolbar=0,status=0,width=800,height=500");
}


// VIDEO FUNCTIONS BEGIN

function fnCalculateTimeWatched(userID,videoID) {
    if ($f() && $f().getState() == 3) $.get("_ajax_video_tracker.php",{u:userID,v:videoID});
}

function fnPosesPopupOn(id) {
    document.getElementById("overlay").style.visibility = "visible";
    self.scrollTo(0, 0);
    document.getElementById("poses_video_title").innerHTML = objVideoData[id][0];
    var file = "/videos/" + objVideoData[id][1];
    $f("poses_video_screen", {src:"flowplayer/flowplayer.commercial-3.1.0.swf",wmode:"opaque"}, {
        key: "#$bdde026021ea801163b",
        canvas: {backgroundColor: "#bfbfbf"},
        clip: {scaling: 'fit'},
        plugins: {
            controls: {
                timeColor: '#ffffff',
                backgroundColor: '#252322',
                sliderColor: '#bfbfbf',
                buttonOverColor: '#728B94',
                progressColor: '#e0e0e0',
                durationColor: '#ffffff',
                borderRadius: '0px',
                bufferGradient: 'none',
                bufferColor: '#787878',
                buttonColor: '#ef5a39',
                sliderGradient: 'none',
                progressGradient: 'medium',
                backgroundGradient: [0.6,0.3,0,0,0],
                opacity: 1.0,
                time: false,
                autoHide: 'always'
            }
        }
    }).play(file);
    document.getElementById("poses_video").style.display = "block";
}

function fnPosesPopupOff() {
    document.getElementById("poses_video").style.display = "none";
    document.getElementById("overlay").style.visibility = "hidden";
    $f().stop();
    $f().stopBuffering();
    $f().unload();
    $f() = null;
}

function fnSongClick(URL) {
    $f().stop();
    $f().stopBuffering();
    window.open(URL);
}

// VIDEO FUNCTIONS END

function fnGenerateCommitmentPic(div,idusers) {
   // $("#"+div).slideUp("slow"); 
    $.ajax({ 
    method: "get",url: "_ajax_get_commitmentpic.php",data: "d="+div+"&f="+idusers, 
    //beforeSend: function(){$("#"+div).show("fast");}, 
    success: function(html){ //so, if data is retrieved, store it in html 
    //$("#"+div).show("slow"); //animation 
    $("#"+div).html(html); //show the html inside div 

  }
  }); //close $.ajax( 
 
}

function fnGenerateProfilePic(div,idusers){
   // $("#"+div).slideUp("slow"); 
    $.ajax({ 
    method: "get",url: "_ajax_get_fbprofilepic.php",data: "d="+div+"&f="+idusers, 
    //beforeSend: function(){$("#"+div).show("fast");}, 
    success: function(html){ //so, if data is retrieved, store it in html 
   // $("#"+div).show("slow"); //animation 
    $("#"+div).html(html); //show the html inside  div 
  }
  }); //close $.ajax( 
 
}

function changeGoToRegistration(){
var goto;
goto = document.mydb_addRecord.goto.value = 'paynow.php';
//alert(goto);
}

function addDiscussion(video){
  var discussion = $("#discussion").val();
  if(discussion != ""){
    $.ajax({ 
    method: "get",url: "_ajax_save_discussion.php",data: "v="+video+"&d="+escape(discussion), 
        success: function(html){ //so, if data is retrieved, store it in html 
         $("#video_discuss").show("slow"); //animation 
        $("#video_discuss").html(html); //show the html inside  div 
       }
    });
  }
}

function getMoreComment(video,startpointer){
    $.ajax({ 
    method: "get",url: "_ajax_get_more_discussion.php",data: "v="+video+"&s="+startpointer, 
        success: function(html){ //so, if data is retrieved, store it in html 
         $("#video_discuss_item_more_"+startpointer).show("slow"); //animation 
        $("#video_discuss_item_more_"+startpointer).html(html); //show the html inside  div 
       }
    });
}

function showHideSearchOpt(){
  $("#search_opt").slideToggle("slow");
}

//]]>