/* Javascript Document
*
*	Copyright 2009 Correlation Inc.
*	Correlation Inc. is a Corporation in the State of New York, USA.
*	all rights reserved
*/

// make custom namespace
(function(){
	
	if(!window.crlw){
		window.crlw = {};
	}
	
	//
	window['crlw']['pagination'] = {
		// initial variables
		list:[],
		count:0,
		// create new pagination
		//
		//<(
		createInst:function(args_arr){
			if(window.crlw.pagination){
				master = window.crlw.pagination;
			}else{
				return false;
			}
			var newInst = {}
			newInst.total = args_arr.total;
			newInst.inst_jsid = master.count;
			master.count++;

			newInst.contentInfo = [];
			var i = 1;
			var curSegment = null;
			while(i <= Number(args_arr.total)){
				//alert(args_arr.segment_htmlid_str);
				// set page html references
				if(args_arr.segments[i]){
					curSegment = args_arr.segments[i];
					newInst.contentInfo[i] = new Array();
					newInst.contentInfo[i]['link_ref'] = document.getElementById(args_arr.link_htmlid_str + i);
					newInst.contentInfo[i]['segment_ref'] = document.getElementById(args_arr.segment_htmlid_str + i);
					//alert(args_arr.segment_htmlid_str + i + ' ' + newInst.contentInfo[i].segment_ref);
					if(newInst.contentInfo[i].link_ref && newInst.contentInfo[i].segment_ref && newInst.contentInfo[i].link_ref.nodeType == 1){
						newInst.contentInfo[i].link_ref['page_no'] = i;
						newInst.contentInfo[i].link_ref['inst_jsid'] = master.count;
						newInst.contentInfo[i].link_ref.onclick = function(){
							if(window.crlw.pagination.goToPage(this.inst_jsid,this.page_no)){
								return false;
							}else{
								return true;
							}
						}
					}
				}
				i++;
			}
			newInst['prev_ref'] = document.getElementById(args_arr.link_htmlid_str + 'prev');
			// alert(newInst.prev_ref);
			newInst.prev_ref['inst_jsid'] = master.count;
			newInst.prev_ref['page_no'] = null;
			if(newInst.prev_ref && newInst.prev_ref.nodeType == 1){
				newInst.prev_ref.onclick = function(){
					if(window.crlw.pagination.goToPage(this.inst_jsid,this.page_no)){
						return false;
					}else{
						return true;
					}
				}
			}
			newInst['next_ref'] = document.getElementById(args_arr.link_htmlid_str + 'next');
			// alert(newInst.next_ref);
			newInst.next_ref['inst_jsid'] = master.count;
			newInst.next_ref['page_no'] = null;
			if(newInst.contentInfo[2]){
				newInst.next_ref['page_no'] = 2;
			}
			if(newInst.next_ref && newInst.next_ref.nodeType == 1){
				newInst.next_ref.onclick = function(){
					if(window.crlw.pagination.goToPage(this.inst_jsid,this.page_no)){
						return false;
					}else{
						return true;
					}
				}
			}
			newInst.link_className = args_arr.link_className;
			newInst.link_activeClassName = args_arr.link_activeClassName;
			newInst.segment_className = args_arr.segment_className;
			newInst.segment_activeClassName = args_arr.segment_activeClassName;
			newInst.prevNext_className = args_arr.prevNext_className;
			newInst.prevNext_inactiveClassName = args_arr.prevNext_inactiveClassName;
			newInst.current = 1;
			master.list[master.count] = newInst;
			master.count++;
			
		},//)>
		//<(
		goToPage:function(inst_jsid,page_no){
			// testing code
			// alert('goto ' + page_no);
			if(window.crlw.pagination){
				var master = window.crlw.pagination;
			}else{
				return false;
			}
			var instance = master.list[inst_jsid];
			// change the page if different than the current page
			if(page_no != instance.current && (page_no != null)){
				if(!master.closePage(inst_jsid,instance.current)){
					alert('problem closepage');
					return false;
				}
				if(!master.openPage(inst_jsid,page_no)){
					alert('problem openpage');
					return false;
				}
				if(!master.setNextPrev(inst_jsid,page_no)){
					alert('problem set next and prev');
					return false;
				}
				return true;
			}else{
				return true;
			}
			return false;
		},
		//)>
		//<(
		closePage:function(inst_jsid,page_no){
			if(window.crlw.pagination){
				var master = window.crlw.pagination;
			}else{
				return false;
			}
			var instance = master.list[inst_jsid];
			if(!(instance.contentInfo[page_no].segment_ref.className = instance.segment_className)){
				alert('problem content segment');
				return false;
			}
			if(!(instance.contentInfo[page_no].link_ref.className = instance.link_className)){
				alert('problem link');
				return false;
			}
			return true;
		},
		//)>
		//<(
		openPage:function(inst_jsid,page_no){
			if(window.crlw.pagination){
				var master = window.crlw.pagination;
			}else{
				return false;
			}
			var instance = master.list[inst_jsid];
			if(!(instance.contentInfo[page_no].segment_ref.className = instance.segment_activeClassName)){
				return false;
			}
			if(!(instance.contentInfo[page_no].link_ref.className =instance.link_activeClassName)){
				return false;
			}
			instance.current = page_no;
			return true;
		},//)>
		//<(
		setNextPrev:function(inst_jsid,page_no){
			if(window.crlw.pagination){
				var master = window.crlw.pagination;
			}else{
				return false;
			}
			var instance = master.list[inst_jsid];
			var prev = null;
			var next = null;
			var max = instance.total;
			if(instance.current > 1 ){
				prev = (instance.current - 1);
			}
			if(instance.current < max){
				next = (instance.current + 1);
			}
			// testing code
			//alert('current: ' +instance.current + ' next: ' + next + ' max: ' + max);


			if(prev == null){
				if(!(instance.prev_ref.className = instance.prevNext_inactiveClassName)){
					alert('prev null error set prev class');
					return false;
				}
			}else{
				if(!(instance.prev_ref.className = instance.prevNext_className)){
					alert('prev notnull error set prev class');
					return false;
				}
			}

			if(next == null){
				if(!(instance.next_ref.className = instance.prevNext_inactiveClassName)){
					alert('next null error set next class');
					return false;
				}
			}else{
				if(!(instance.next_ref.className = instance.prevNext_className)){
					alert('next notnull error set next class');
					return false;
				}
			}
			instance.prev_ref.page_no = prev;
			instance.next_ref.page_no = next;
			return true;
		}
		//)>
	}


})();

