﻿// initialise plugins
jQuery(function(){
	jQuery('ul.sf-menu').superfish();	
	initSideMenu();
});

function pageLoad() { 
    // Initialization code here, meant to run once after each postback also. 
    ajaxMouseOverRollEffect();
    ajaxClickActionEffect();
    fancyBoxInit();
}
function initSideMenu() {
	
    $('#sideMenu ul').hide();
    $('#sideMenu li a').mouseover(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#sideMenu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
}

$(document).ready(function() {
    $('.OnClickNavigateUrl').click( function() {
        window.location.href = $(this).attr('href');
        return false;
    });
});

$(document).ready(function() {
  $('.rollover').hover(function() {
	    var currentImg = $(this).attr('src');
	    $(this).attr('src', $(this).attr('hover'));
	    $(this).attr('hover', currentImg);
    }, function() {
	    var currentImg = $(this).attr('src');
	    $(this).attr('src', $(this).attr('hover'));
	    $(this).attr('hover', currentImg);
    });    
});

function ajaxMouseOverRollEffect () 
{
  $('.ajaxRollover').mouseover(function() {
        var currentDiv = $(this).attr('divId'); // get the popup div id
        var offset = $(this).offset(); // get the positions
        var width = $(this).width(); // get the object calling width
        
        if ( offset.left > 900 ) // if the pop is going off the page width
            offset.left = offset.left - 2 * width;
        $("#" + currentDiv).css( { "left": (offset.left) + "px", "top":offset.top + "px" } );    // get the popup div position
        
        // on mouse out of the div, hide it
        $("#" + currentDiv).mouseout( function() {
            setInterval( $(this).hide(), 1000);
        });
        
        // on mouseover the div, keep showing it
        $("#" + currentDiv).mouseover( function() {
            $(this).show();
        });
       
       // get the ajax content
       $.ajax({
          url: baseURl + "/htmlPages/Ajax/" + $(this).attr('ajaxURl'),
          data: "url=" + window.location.href,
          cache: false,
          success: function(html){           
            $("#" + currentDiv).text(""); // used to nullify the current contents of the div.
            $("#" + currentDiv).show("slow");
            $("#" + currentDiv).append(html);
          }
        });
    });    
}

function ajaxClickActionEffect() 
{
  $('.ajaxCommentAction').click(function() {
        var javaScriptAction = $(this).attr('onSuccessAction'); // get the popup div id
        var confirmMessage = $(this).attr('confirmMessage');
        
        $.fancybox(
            '<div class="ResultMessage-Blank"><p><img align="left" src="' + baseURl + '/library/images/ajax-loader.gif" style="margin-right:10px;" /><h1 class="BlueNoBorder">Working on your request</h1></p></div>',
            {
                'autoDimensions'	: false,
                'width'         		: 350,
                'height'        		: 45,
                'transitionIn'		: 'none',
                'transitionOut'		: 'none'
            }
        );
           
        alert($(this).attr('ajaxURl'));   
        // get the confirmationfirst
        if ( confirm(confirmMessage )) 
        {
            // do the ajax action           
        
            $.ajax({
              url: baseURl + "/htmlPages/Ajax/" + $(this).attr('ajaxURl'),
              data: "url=" + window.location.href,
              cache: false,
              success: function(data){
                if ( data == "Success") {           
                    $.fancybox.close();
                    refreshPage(javaScriptAction);
                }
                else
                {
                    $.fancybox.close();
                    $.fancybox(
	                    '<div class="ResultMessage-Failure">Opps! Something went wrong, please try again later.</div>',
	                    {
    		                'autoDimensions'	: true,
		                    'width'         		: 350,
		                    'height'        		: 'auto',
		                    'transitionIn'		: 'none',
		                    'transitionOut'		: 'none'
	                    }
                    );
                }
              }
            });
        }
        
        else {
            $.fancybox.close();
            return false;             
            }
       
    });    
    
  $('.ajaxAction').click(function() {
        var javaScriptAction = $(this).attr('onSuccessAction'); // get the popup div id
       
       // get the ajax content
       $.ajax({
          url: baseURl + "/htmlPages/Ajax/" + $(this).attr('ajaxURl'),
          data: "url=" + window.location.href,
          cache: false,
          success: function(data){
            if ( data == "Success") {           
                eval(javaScriptAction);
            }
            else
            {
                $.fancybox(
	                '<div class="ResultMessage-Failure">Opps! Somethign went wrong, please try again later.</div>',
	                {
    		            'autoDimensions'	: true,
		                'width'         		: 350,
		                'height'        		: 'auto',
		                'transitionIn'		: 'none',
		                'transitionOut'		: 'none'
	                }
                );
            }
          }
        });
    });    
}

$(function() { 
    $('.highlight tr').hover(function() { 
        $(this).css('background-color', '#f6f6f6'); 
    }, 
    function() { 
        $(this).css('background-color', '#FFFFFF'); 
    }); 
}); 

comment_ReplyToId = 0;

/* fancy box functions */
function fancyBoxInit() {

    $(".ReplyToComment").click( function() {
        // get the comment id
        comment_ReplyToId = $(this).attr('commentId');
    });
    
    $(".ReplyToComment").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
	        comment_ReplyToId = 0;
        }
    });
    
    $(".SuggestTopic").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $(".RequestJoin").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $(".SubmitArticle").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $(".DashboardExerciseLog").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $(".DashboardContactDietician").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $(".DashboardPostReminder").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            //nothing
        },
        'onClosed'		: function() {
	        // nothing
        }
    });
    
    $("#nvAboutUs").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'title'			: this.title,
			'width'			: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			    'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

		return false;
	});
	
	$("#NikhylsSwimathon").click(function() {
	    $.fancybox.showActivity();
	    $.ajax({
		    type		: "POST",
		    cache	: false,
		    url     : baseURl + "/htmlPages/Ajax/" + $('#NikhylsSwimathon').attr('ajaxURl'),
		    success: function(data) {
			    $.fancybox(data);
		    }
	    });

	    return false;
    });
    
    var NikhylsSponsor_form_validator = $("#NikhylsSponsor_form").validate({
        rules: {
	        ContactName: "required",
	        txtLaps: {
	            required: true,
	            number: true
	        },
	        txtEmails: {
		        required: true,
		        email: true
	        }
        },
        onkeyup: false,
        messages: {
	        ContactName: "Please enter your contact name",
	        txtEmails: "Please enter a valid email address",
	        txtLaps: "Please enter the number of laps you want to sponsor"
        },
        errorClass : 'modal_error',
        errorPlacement: function(label, element) {
			label.insertAfter(element);
		}
    });
           
    
    $(".NikhylsSponsor").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            NikhylsSponsor_form_validator.resetForm();
        },
        'onClosed'		: function() {
	        NikhylsSponsor_form_validator.resetForm();
        }
    });

    $("#NikhylsSponsor_form").bind("submit", function() {

        if ( NikhylsSponsor_form_validator.valid() == false) 
        {
            $.fancybox.resize();
            return false;
        }
        
        $.fancybox.resize();        
        $.fancybox.showActivity();

	    $.ajax({
		    type		: "POST",
		    cache	: false,
		    url		: baseURl + "/htmlPages/Ajax/NikhylSponsor.aspx",
		    data		: $(this).serializeArray(),
		    success: function(data) {
			    $.fancybox(data);
		    }
	    });

        return false;
    });
    
    var NewsletterSendToFriend_form_validator = $("#NewsletterSendToFriend_form").validate({
        rules: {
	        txtContactName: "required",
	        txtEmail: {
		        required: true,
		        email: true
	        }
        },
        onkeyup: false,
        messages: {
	        txtContactName: "Please enter your contact name",
	        txtEmail: "Please enter a valid email address"
        },
        errorClass : 'modal_error',
        errorPlacement: function(label, element) {
			label.insertAfter(element);
		}
    });
           
    
    $(".NewsletterSendToFriend").fancybox({
	    'scrolling'		: 'no',
	    'titleShow'		: false,
	    'onComplete'    : function() 
        {
            NewsletterSendToFriend_form_validator.resetForm();
        },
        'onClosed'		: function() {
	        NewsletterSendToFriend_form_validator.resetForm();
        }
    });

    $("#NewsletterSendToFriend_form").bind("submit", function() {

        if ( NewsletterSendToFriend_form_validator.valid() == false) 
        {
            $.fancybox.resize();
            return false;
        }
        
        $.fancybox.resize();        
        $.fancybox.showActivity();

	    $.ajax({
		    type		: "POST",
		    cache	: false,
		    url		: baseURl + "/htmlPages/Ajax/SendNewsletterToFriend.aspx",
		    data		: $(this).serializeArray(),
		    success: function(data) {
			    $.fancybox(data);
		    }
	    });

        return false;
    });
}

function openBareCenteredWin(newUrl, winName, winWidth, winHeight) {
    __openWin(newUrl, winName, winWidth, winHeight, ",status=no,scrollbars=yes,resizable=yes,menubar=no,toolbar=no");
}

function __openWin(newUrl, winName, winWidth, winHeight, strOptions) {
    var str;
    str="width=" + winWidth + ",height=" + winHeight + ",innerHeight=" + winHeight;
    str+=",innerWidth=" + winWidth;

    if (window.screen)
    {
      var yPos = ( screen.availHeight / 2 ) - ( winHeight / 2);
      yPos = yPos < 0 ? 0 : yPos;
      var xPos = ( screen.availWidth / 2 ) - ( winWidth / 2);
      xPos = xPos < 0 ? 0 : xPos;
      str += ",left=" + xPos;
      str += ",screenX=" + xPos;
      str += ",top=" + yPos;
      str += ",screenY=" + yPos;
    }
    str += strOptions;
    PopUp(newUrl, winName, str);
}

function PopUp(sURl, sName, sFeatures) {
	// To open a new window

		var newBrowser;
		newBrowser = window.open(sURl, sName, sFeatures);
		newBrowser.focus();		
}

function PrintPage ( locURl ) 
{
    openBareCenteredWin(baseURl + "/Print-View/" + locURl, "PrintPage", 800,600);
}

function PrintProgressReport( ) 
{
    var locURl = baseURl + "/Dashboard/Report.aspx";
    openBareCenteredWin(locURl + "?IsPrint=1", "PReport", 800,600);
}
function PrintSymptomsReport( selectedDate ) 
{
    var locURl = baseURl + "/Dashboard/Symptoms-Report.aspx?Id=";
    openBareCenteredWin(locURl + selectedDate + "&IsPrint=1", "SPReport", 600,600);
}

function PrintNutritionalAdequacy( adequacyId, type ) 
{
    var locURl = baseURl + "/Dashboard/Nutritional-Adequacy.aspx?Id=";
    
    if ( type != "C")
        locURl = baseURl + "/Dashboard-Nutritionists/Nutritional-Adequacy.aspx?Id=";
    
    locURl += adequacyId + "&IsPrint=1";
    if ( PrintNutritionalAdequacy.arguments.length == 3 ) 
        locURl += "&consumerId=" + PrintNutritionalAdequacy.arguments[2];
    
    openBareCenteredWin(locURl, "PNutritionalAdequacy", 800,600);
}

function PrintDailySymptoms( rowId, type ) 
{
    var locURl = baseURl + "/Dashboard/Daily-Diary-Symptoms.aspx";
    if ( type != "C")
        locURl = baseURl + "/Dashboard-Nutritionists/Daily-Diary-Symptoms.aspx";
    
    locURl += "?Id=" + rowId + "&IsPrint=1";
    if ( PrintDailySymptoms.arguments.length == 3 ) 
        locURl += "&consumerId=" + PrintDailySymptoms.arguments[2];
        
    openBareCenteredWin(locURl, "PDailyDairySymptons", 800,600);
}

function Rate(itemId, moduleId, Rating)
{
    // get the ajax content
   $.ajax({
      url: baseURl + "/Rating.ashx",
      data: "rowId=" + itemId + "&moduleId=" +  moduleId + "&rating=" + Rating,
      cache: false,
      success: function(data){
        $.fancybox(
            '<div class="ResultMessage-Blank">' + data + '</div>',
            {
	            'autoDimensions'	: false,
                'width'         	: 340,
                'height'        	: 216,
                'transitionIn'		: 'none',
                'transitionOut'		: 'none'
            }
        );
      }
    });
}

function refreshPage ( refreshUrl )
{
    window.location.href = refreshUrl;
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

