//________________Pre-amble_____________________________________________
//Latest Version: 0.23 | Updated: 21/6/2012
//Coded by: Mohd Amir Shah, Design & Multimedia Department
//
//________________Changelogs____________________________________________
//#V0.01 - ready() for basic navigation + content loader + preloader
//       - new navigation button design next_btn,prev_btn,print_btn,list_btn
//#V0.02 - animated content loader rendering fadeIn() + fadeOut()
//#V0.03 - <div></div> simplified on content loader
//#V0.04 - improved content loader navigation
//#V0.04 - slideToggle() implemented
//#V0.05 - mouseleave() implemented
//#V0.06 - slideToggle improved | jQuery.js
//#V0.07 - mouseleave() improved | jQuery.js
//#V0.08 - code housekeeping/clean-up/simplified
//#V0.09 - init inner function - subpageArr[num] for subpages
//#V0.10 - differentiate from mainpage navigation and subpage navigation indicator
//#v0.11 - reset subpage re-entry for cursubpage=0
//       - pre-emptive stop/click(false) for initial subpage=1 OR subpage=subpageArr[cursubpage]
//       - improved subpage navigation detection using subpageArr[curpage-1]
//#v0.12 - readable Arabic character | Unicode-ISO and charset=UTF-8
//       - improved brake-point for arabic joining character
//       - improved alignment for bullet-point and sub-content
//       - improved sub-topic navigation (subnum1_btn..subnum27_btn)
//#v0.13 - improved rollOver list-menu/graphic for menu/css style
//		 - improved list-menu toggle()
//#v0.14 - flash embed with jQuey tools | toolbox.flashembed.min.js
//		 - list-menu auto scrollable | overflow:auto
//		 - slideToggle() removed | implement absolute
//#v0.15 - implement subnavigation function
//       - implement subtopic_btn
//       - print_btn activate
//#v0.16 - fixed subtopic_btn major bugs
//       - fixed compressed javascript bugs
//		 - improve embedded flash object | toolbox.flashembed.min.js removed!
//#v0.17 - implement font detection | arabic.js - v 0.1
//#v0.18 - improved font detection with multiple font comparison | arabic.js - v0.2
//#v0.19 - implement donwload-on-demand (DOD)| fonts.zip | arabic.js - v0.3
//#v0.20 - improved DOD with detection and keyboard detection | arabic.js - v0.4
//#v0.21 - improved navigation bookmarking
//		 - improved detection on visited link
//		 - improved on title bg image for multi-browser
//       - improved main navigation indicator 
//       - improved sub navigation indicator 
//#v0.22 - improved and implement new design
//       - improved on note-it function for each pages
//       - create marquee.js for animated scrolling title
//       - added display subpage numbering
//       - improvised guideline pages
//#v0.23 - improved subpage navigation counter
//       - graphic enabled for arabic character
//       -
//______________________________________________________________________

$(document).ready(function(){
    //References
	var loading = $("#loading");
	var content = $("#content");
	var subnext = $("#subNext_btn");
	var subprev = $("#subPrev_btn");
	var np = $("#np");
	var noteit_txt = $("noteit_txt");
	//initial starting current page
	var curpage=1;
	//including user guide page
	var totalpage=18; 
	//total available subtopic page btn which is maximum 27 button already inplace in index.html
	var totalsubnum=27; //temp not use!
	//store no. of subpage for available lesson
	//temp var Array ----------1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
	var subpageArr = new Array(2,0,0,0,4,0,3,0,3,0,2,0,3,0,0,0,0,0);
	//store availability. of notepage for available lesson - 0: Not Available
	//temp var Array -----1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
	var npArr = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	//store no. of subtopic for available lesson
	//temp var Array -----------1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7--------disable for the time being - Nurus
	var subtopicArr = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	//store no. of notepage for available lesson
	//temp var Array -----------1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
	/*Disable for arabic - not suitable for arabic character
	var notepageArr = new Array("this is sample of Note-It",// page 1
								0,
								0,
								0,
								0,
								0,
								"this is sample of Note-It",
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0);*/ 
	var cursubpage = 1;
	var totalnoteit = npArr[curpage-1];
  	var totalsubpage = subpageArr[curpage-1];
	var cursubtopic = cursubpage;
	//load user guide - initial load| file: userguide.html
	content.load("page1.html #subpage1",hideLoading);
	//initial load subpage display
	subpageDisplayNum();
	//load initial bookmark | .quickjump menu
	$('#1').css("color","#bb0101");
	$('img#i1').attr("src","images/tick-ico.png");
	//init noteit_btn
	// faikam hide noteit button
	//mainNavState();
	content.css("display", "none");
    content.fadeIn(500);
	subnext.css("display", "none");
	subnext.fadeIn(500);
    subprev.css("display", "none");
	subprev.fadeIn(500);
	clearSubTopic();
    $("#list_btn_menu").css("display", "none");
  	$("#noteit_btn").css("display", "none");
	$("#lesson_box").css("display", "none");
   	
	//mainpage foward btn
	$("#fwd_btn").hover(
				function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	$("#fwd_btn").mouseleave(
				function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
	$("#fwd_btn").click(function(){
		cursubpage=1;
		if (curpage<totalpage){curpage++;}
		subPageCheck();
		showLoading();
        subNavigation();
		visitedLink();
		mainNavState();
		subNavState();
		subpageDisplayNum();
	});
	//mainpage back btn
	$("#bck_btn").hover(function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	$("#bck_btn").mouseleave(function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
	$("#bck_btn").click(function(){
		cursubpage=1;
		if (curpage>1){curpage--;}
	    subPageCheck();
		showLoading();
		subNavigation();
		visitedLink();
		mainNavState();
		subNavState();
		subpageDisplayNum();
	});
	//list btn
	$("#list_btn").hover(function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	$("#list_btn").mouseleave(function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
	$("#list_btn").click(function () {
	$("#list_btn_menu").toggle();
    });
	$('#list_btn_menu').mouseleave(function() {
    $("#list_btn").click();
    });
	
	//lesson title
	$("#lesson_btn").click(function(){
	$("#lesson_box").toggle();
	});
	$("#lesson_box").mouseleave(function() {
    $("#lesson_btn").click();
    });
	$("#content").hover(function(){
	$("#lesson_box").hide();
	});
	
	//print btn
	$("#print_btn").hover(function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	$("#print_btn").mouseleave(function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
	$("#print_btn").click(function(){
		//print content
		window.print();
	});

	//subpage navigation
	subnext.hover(function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	subnext.mouseleave(function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
     subnext.click(function(){
		if (cursubpage<subpageArr[curpage-1]){
			cursubpage++;
			npload = true;
			npLoader();
			 content.load("page"+curpage+".html #subpage"+cursubpage, hideLoading);
			 //notepage loader
			
		   }else if(cursubpage==subpageArr[curpage-1]){
			   //code here
			}
	    content.css("display", "none");
        content.fadeIn(500);
		subNavState();
		subpageDisplayNum();
    });
	 //sub prev button
	 subprev.hover(function () {
                $(this).css("cursor", "pointer");
				$(this).css("background-position", "bottom");
				}
	);
	subprev.mouseleave(function () {
                //$(this).css("cursor", "pointer");
				$(this).css("background-position", "top");
				}
	);
	subprev.click(function(){
      if (cursubpage>1){
		  cursubpage--;
		  npload = true;
		  npLoader();
		  content.load("page"+curpage+".html #subpage"+cursubpage, hideLoading);
		  //notepage loader
		  
		 // subprev.show();
		  }/*else if(cursubpage==1){
		 //subprev.click().remove();
		// subprev.hide();
		}*/
	    content.css("display", "none");
        content.fadeIn(500);
		subNavState();
		subpageDisplayNum();
		 
    });
	function showLoading(){
		loading
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(500, 0);
		
	}
	//init hide - btn
	function clearSubTopic(){
		var i=0;
		for(i=0;i<totalsubnum+1;i++){
			$("#subnum"+i+"_btn").css("display", "none");
		}
	}
	$("a.transition").click(function(event){
		event.preventDefault();
		
	});
	//quickjump menu
	$('.quickjump').click(function(){   
        var target = $(this).attr("id");
		// script below add by faikam to set back the next sub page to starting sub 
		// previous have bug where it dosen't reset to start sub page when navigate using list menu instead of next / prev UI button
		cursubpage=1;
		
		// end of faikam edit script
        curpage = target;
        subPageCheck();
        showLoading();
        subNavigation();
        visitedLink();
		mainNavState();
		subNavState();
		subpageDisplayNum();
		//npLoad=true;
		
		
     });
	function visitedLink(){
	$('#'+curpage).css("color","#bb0101");
	var imgtemp = $('img#i'+curpage).attr("src");
		if(imgtemp == "images/folder-ico.png" || imgtemp == "images/folder-tick-ico.png"){
			$('img#i'+curpage).attr("src", "images/folder-tick-ico.png");
		}else{
			$('img#i'+curpage).attr("src", "images/tick-ico.png");
	  }
	  
	}
	//subpage enable btn check
	function subPageCheck(){
		 if(subpageArr[curpage-1]==0){
            subnext.css("display", "none");
      	    subprev.css("display", "none");
			$("#subpageDisable").hide();
            }else{
                subnext.css("display", "none");
                subprev.css("display", "none");
                subnext.fadeIn(500);
                subprev.fadeIn(500);
				$("#subpageDisable").show();
        }
		 
	}
	  // Navigation for subpages & subtopics
	 function subNavigation(){
		if(subpageArr[curpage-1]!=0 || subtopicArr[curpage-1]!=0){
        	content.load("page"+curpage+".html #subpage"+cursubpage, hideLoading);
			npload = true;
			}else{
            content.load("page"+curpage+".html #subpage0", hideLoading);
			npload = false;
			
        }
		npLoader();
		content.css("display", "none");
        content.fadeIn(500);
        //goto subtopic
        if(subtopicArr[curpage-1]!=0){
			subtopicNavState();
            var j=0;
            for(j=0;j<subtopicArr[curpage-1]+1;j++){
                $("#subnum"+j+"_btn").css("display", "none");
                $("#subnum"+j+"_btn").fadeIn(500);
				//IE9 hover effect
				$("#subnum"+j+"_btn").hover(
							function () {
                			$(this).css("cursor", "pointer");
							$(this).css("background-position", "bottom");
							}
				);
				$("#subnum"+j+"_btn").mouseleave(
							function () {
               				 //$(this).css("cursor", "pointer");
							$(this).css("background-position", "top");
							}
				);
				// end of IE9 hover
                //-----subTopic_btn
             (function(k) {
            $("#subnum"+k+"_btn").click(function() {
			  cursubpage = k;
			  subtopicNavState();
              content.css("display", "none");
              content.fadeIn(500);
              content.load("page"+curpage+".html #subpage"+k, hideLoading);
			  npload = true;
			  npLoader();
           		 });
       		 })(j);
                }
            }else{
                clearSubTopic();
        }
	
	}
	mainNavState();
	function mainNavState(){
		if(curpage == 1){$("#bck_btn").hide();}else{$("#bck_btn").show();}
		if(curpage == totalpage){$("#fwd_btn").hide();}else{$("#fwd_btn").show();}
		
	}
	subNavState();
	function subNavState(){
		if (cursubpage<subpageArr[curpage-1]){
			subnext.show();
		}else if(cursubpage==subpageArr[curpage-1]){
			subnext.hide();
		}
		if (cursubpage>1){
		  subprev.show();
		  }else if(cursubpage==1){
		 subprev.hide();
		}
	}
	function subtopicNavState(){
		//alert("subtopicArr[curpage-1]+1 : "+subtopicArr[curpage-1]);
		enableSubTopic();
		$("#subnum"+cursubpage+"_btn").css({
							  //'position':'absolute',
							  'background-image' : 'url(images/no/no'+cursubpage+'_ar_dis.png)'
							  //'opacity' : '0.25'
							  //'background-position' : 'bottom'
							  });
	}
		function enableSubTopic(){
		var l=0;
		for(i=0;l<subtopicArr[curpage-1]+1;l++){
			//$("#subnum"+l+"_btn").fadeIn(500);
			$("#subnum"+l+"_btn").css({
							  		  //'position':'absolute',
							  		 'background-image' : 'url(images/no/no'+l+'_ar.png)'
							 		 //'background-position' : 'bottom'
									 });
		}
	}
	//pagenation display for subpage - arabic character
	function subpageDisplayNum(){
		
		if(subpageArr[curpage-1]!=0){
			
			$("#subpage_num_display").text("");
			//$("#subpage_num_display").append("Page : "+cursubpage+"/"+subpageArr[curpage-1]);
			
			//arabic numbering for subpage counter - amir
			// image page.png remove by faikam . Below is the original line
			//$("#subpage_num_display").append('<img src="images/no/ar/'+subpageArr[curpage-1]+'.png" /><img src="images/no/ar/slash.png" 
			$("#subpage_num_display").append('<img src="images/no/ar/'+subpageArr[curpage-1]+'.png" /><img src="images/no/ar/slash.png" /><img src="images/no/ar/'+cursubpage+'.png" />');

		}else{
				$("#subpage_num_display").text("");
			}
			
		}
	
	
	//animated main title | index.html
		$('.animated_title').SetScroller({	velocity: 	 60,
											direction: 	 'horizontal',
											startfrom: 	 'left',
											loop:		 'infinite',
											movetype: 	 'linear',
											onmouseover: 'pause',
											onmouseout:  'play',
											onstartup: 	 'play',
											cursor: 	 'pointer'
										});

//--------Notepage button-----------
	$("#noteit_btn").hover(function () {$(this).css("cursor", "pointer");$(this).css("background-position", "bottom");});
	$("#noteit_btn").mouseleave(function () {$(this).css("background-position", "top");});

	$("#noteit_btn").click(function(){
		centernoteit();
		loadnoteit();
		hideslider();
	});
	$("#noteitContactClose").click(function(){
		disablenoteit();
	});
	$("#backgroundnoteit").click(function(){
		disablenoteit();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && noteitStatus==1){
			disablenoteit();
		}
	});
	
	function npState(){
	if ((npArr[curpage-1] == 7 && cursubpage == 3) ||(npArr[curpage-1] == 13 && cursubpage == 2)){
   			$("#noteit_btn").show();
 			} else {
    		$("#noteit_btn").hide();
 		 }
	
	}
		
	//Notepage Loader/filter
	function npLoader(){
		//alert("cursubpage :"+cursubpage);	
		if(npload == true){//
			$("#noteit_txt").load("page"+curpage+".html #np"+cursubpage);
			npState();
				}else{
			$("#noteit_txt").load("page"+curpage+".html #np");
			 npState();
		}
		//alert("$([id^=np]).html() : "+$("[id^=np]").html());
		
	}
});