//Nivo slider
$(window).load(function() {
    $('#slider').nivoSlider({
        effect:'random', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:500, // Slide transition speed
        pauseTime:3000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:false, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:true, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
});

// Popup dialog windows
$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$( "#dialog:ui-dialog" ).dialog( "destroy" );

	$("#tutorial-modal").dialog({ 
		autoOpen: false,
		width: 775,
		modal: true
	});  
	$("#faq-modal").dialog({ 
		autoOpen: false,
		width: 850,
		height: 550,
		modal: true
	});  
	$("#priv-modal").dialog({ 
		autoOpen: false,
		width: 850,
		modal: true
	});  
	
	$("#open_tut").click(function() {  
	  $("#tutorial-modal").dialog("open");  
	}); 
	$("#open_tut2").click(function() {  
	  $("#tutorial-modal").dialog("open");  
	}); 
	$("#open_faq").click(function() {  
	  $("#faq-modal").dialog("open");  
	}); 
	$("#open_priv").click(function() {  
	  $("#priv-modal").dialog("open");  
	}); 
	$("#open_priv2").click(function() {  
	  $("#priv-modal").dialog("open");  
	}); 
});

// Quicklink modal dialog popup
	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var llink = $( "#LinkLink" ),
			lurl = $( "#LinkUrl" ),
			licon = $( "#LinkIcon" ),
			lcat = $( "#LinkCategoryId" ),
			allFields = $( [] ).add( llink ).add( lurl ).add( licon ).add( lcat ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " cannot be empty!" );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 450,
			width: 550,
			modal: true,
			buttons: {
				" Create your link! ": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( llink, "Link name", 1, 50 );
					bValid = bValid && checkLength( lurl, "Url", 1, 500 );
					bValid = bValid && checkLength( licon, "Icon", 1, 50 );
					bValid = bValid && checkLength( lcat, "Category", 1, 50 );
					if ( bValid ) {
						$("#LinkAddForm").submit();
						$( this ).dialog( "close" );
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

	  $("#open_link").click(function() {  
		$("#dialog-form").dialog("open");  
	  }); 
	});

// Quicklink modal dialog popup
	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var llink = $( "#LinkLink" ),
			lurl = $( "#LinkUrl" ),
			licon = $( "#LinkIcon" ),
			lcat = $( "#LinkCategoryId" ),
			allFields = $( [] ).add( llink ).add( lurl ).add( licon ).add( lcat ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " cannot be empty!" );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#links-modal" ).dialog({
			autoOpen: true,
			height: 450,
			width: 550,
			modal: true,
			buttons: {
				" Save your link! ": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( llink, "Link name", 1, 50 );
					bValid = bValid && checkLength( lurl, "Url", 1, 500 );
					bValid = bValid && checkLength( licon, "Icon", 1, 50 );
					bValid = bValid && checkLength( lcat, "Category", 1, 50 );
					if ( bValid ) {
						$("#LinkEditForm").submit();
						$( this ).dialog( "close2" );
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
				location.href = document.referrer;
			},
			close2: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
			
		});
	});

//Jquery digital clock
$(function($) {
   var options = {
        timeNotation: '24h',
        am_pm: true,
        fontFamily: 'Verdana, Times New Roman',
        fontSize: '11px',
        foreground: 'white'

      }; 
   $('.jclock').jclock(options);
});

// Insert and edit icon list
function fillIcon(icon){
    // Set form name in variable
    var form = document.forms.LinkAddForm
    //set values
    form.LinkIcon.value = icon;
	document.getElementById('image').innerHTML='<img src="/mypspage/img/icons/'+icon+'"  width="25" height="25" alt="Loading icon… please wait!"/>';
}
function editIcon(icon){
    // Set form name in variable
    var form = document.forms.LinkEditForm
    //set values
    form.LinkIcon.value = icon;
	document.getElementById('image').innerHTML='<img src="/mypspage/img/icons/'+icon+'"  width="25" height="25" alt="Loading icon… please wait!"/>';
}

function setHomePage(userid)
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
  document.body.setHomePage('http://www.mypspage.com/baked/homepage/'+userid);
 
    }
    else if (window.sidebar)
    {
    if(window.netscape)
    {
         try
   {  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         }  
         catch(e)  
         {  
    alert("this action was aviod by your browser，if you want to enable，please enter about:config in your address line,and change the value of signed.applets.codebase_principal_support to true");  
         }
    } 
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage','http://www.mypspage.com/baked/homepage/'+userid);
 }
}
