//<![CDATA[
$(document).ready(function() {
    $("#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);
          var html = '<span id=""><img src="images/save2favorites.gif" class="share_icon" width="16" height="16" alt="" /><span class="text_12"><a href="/myaccountfavorites.php">Saved in My Favorites</a></span></span>';
          $("#parent_section").html(html);
    });
    $("#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 time_practiced = $("#time").val();
        time_practiced = time_practiced.toString();
        

        var time_array = new Array();
        
        if(time_practiced.indexOf(",") >= 0 ){
            time_array = time_practiced.split(","); 
        }else if(time_practiced.indexOf(":") >= 0){
            time_array = time_practiced.split(":"); 
        }else if(time_practiced.indexOf(".") >= 0){ 
            time_array = time_practiced.split("."); 
        }else{
            var time_added = parseInt(time_practiced) ;
        }        
     
        
        
        if(time_array.length > 0 ){ 
              first_element = parseInt(time_array[0]);//alert(first_element);
              second_element = parseInt(time_array[1]);//alert(second_element);
              
              if(isNaN(second_element)){
                  second_element = 0 ;
              }else{ 
                 var len_second_element = second_element.toString().length ;
                 if(len_second_element == 1 )
                 second_element = second_element*6;
              }    
              
              if(isNaN(first_element)){
                  var tot_mins = 0 ;
                  tot_mins = tot_mins + second_element ;
              }else if(first_element >= 5 ){
                  var tot_mins = first_element ;                  
                 // tot_mins = tot_mins + second_element ;
                //  alert(tot_mins);
              }else if(first_element < 5){
                  tot_mins = parseInt(60*first_element);
                  tot_mins = tot_mins + second_element ;
              }
              
       }else if(time_added >= 5 ){
            tot_mins = time_added ;
        }else if(time_added < 5 ){
            tot_mins = 60*time_added;
        }
        tot_mins = parseInt(tot_mins);
        
 
        var time = tot_mins ;
        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;
            $("#d"+day).css('color','#ec5a39');
            $("#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");
        $("#suspendreason").slideDown("fast");
    });
    $("input[name='cancelaction']:nth(1)").click(function() {
        $("#suspendreason").slideUp("fast");
        $("#cancelreason").slideDown("fast");
    });*/
   
     $("#cancelaction1").click(function() {
        $("#suspendreason").slideUp("fast");
        $("#cancelreason").slideUp("fast");
        $("#suspenddatereason").slideDown("fast");
    });
    $("#cancelaction2").click(function() {
        $("#cancelreason").slideUp("fast");
        $("#suspenddatereason").slideUp("fast");
        $("#suspendreason").slideDown("fast");
    });
    
    $("#cancelaction3").click(function() {
        $("#suspendreason").slideUp("fast");
        $("#suspenddatereason").slideUp("fast");
        $("#cancelreason").slideDown("fast");
    });
   
    $("#optintext").focus(function(){if(this.value==this.defaultValue){this.value="";this.style.color="#000000";}});
});

function fnCommDelHrs(year, month, day) {
 var oldval = parseFloat($("#d"+day)[0].innerHTML);
 var remins = 0 ;
     $.ajax({ 
        method: "get",url: "_ajax_delete_practicelog.php",data: "y="+year+"&m="+month+"&d="+day, 
        success: function(html){ //so, if data is retrieved, store it in html 
          //alert(html);
          if(html != 'None' ){
              $("#d"+day)[0].innerHTML = html ;
              var remins  = parseFloat(html);
              $("#d"+day).css('color','');
              $("#x"+day)[0].style.visibility = "hidden";
          }else{
              $("#d"+day)[0].innerHTML = "";
              $("#x"+day)[0].style.visibility = "hidden";
              $("#d"+day).css('color','#ec5a39');
             
          }
          
          var final_val = oldval-remins ; 
          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(final_val * -1);
       }
    });
    
    
}


function updatePublishDate(id,videoid){
		var date = $("#pub_date"+id).val();
		var v_id=videoid;
		var time = $("#pub_time_hour_min"+id).val();
	 
		if(date!=""){
				$.ajax({
						method:"get",url:"_ajax_update_publish_date.php",data:"v="+v_id+"&d="+date+"&t="+time});
		}

}





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) {
	try {
        if ($f() && $f().getState() == 3) { $.get("_ajax_video_tracker.php",{u:userID,v:videoID}); }
	} catch(html5) {
        try {
            if (html5_playing) { $.get("_ajax_video_tracker.php",{u:userID,v:videoID}); }
        } catch(failed) {
            return;
        }
	}
}


function fnVideoAccessedHistory(userID,videoID) {
  $.get("_ajax_videoaccess_history.php",{u:userID,v:videoID});
}

//function to update music_click_log table and open the link in another window.
function fnSongClick(URL,idmusic,idvideo) {
 $.ajax({
 method:"get",
 url:"_ajax_update_music_click_log.php",data:"idmusic="+idmusic+"&idvideo="+idvideo 
 }); 
     $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();
  var checked_val = $("#email_follow_up").val();
  if(discussion != ""){
    $.ajax({ 
    method: "get",url: "_ajax_save_discussion.php",data: "v="+video+"&d="+escape(discussion)+"&ch="+checked_val, 
        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");
}

function hideOptInForm() {
    var email = $("#optintext").value;
    if (email != "") {
    	$.get("_ajax_hide_optin.php");
    }
}

function checkuncheckNotifyEmail(video){
    var checked_val;
      if(document.getElementById("email_follow_up").checked){
	    document.getElementById("email_follow_up").checked = false ;
	    checked_val = 'No';
      }else{
	    document.getElementById("email_follow_up").checked = true ;
	    checked_val = 'Yes';
      }
    $.ajax({ 
    method: "get",url: "_ajax_save_discussion_subscribe.php",data: "v="+video+"&ch="+checked_val, 
        success: function(html){ //so, if data is retrieved, store it in html 

       }
    });
}

function subscribeUnsubscribeNotifyEmail(video){
	var checked_val;
	if(document.getElementById("email_follow_up").checked) {
	    checked_val = 'Yes';
	} else {
	    checked_val = 'No';
	}

    $.ajax({ 
    method: "get",url: "_ajax_save_discussion_subscribe.php",data: "v="+video+"&ch="+checked_val, 
        success: function(html){ //so, if data is retrieved, store it in html 

       }
    });
}

//]]>

