﻿var slideMenu2=function(){
	var sp2,st2,t2,m2,sa2,l2,w2,sw2,ot2, pos;
	var currentBgPos = '0 0';
	var prevCurrent;
	
	/*
		m2 -> slider ul element
		sa2 -> 
	*/
	return{
		build:function(preview_slider,sw2,mt2,s2,sl2,h2, aa1){
			isInit = false;
			al = arguments.length;
			for (var i = 0; i < al; i++) {
				if (arguments[i] == 'init') {
					isInit = true;
					break;
				}
			}			
			
			pos=aa1;
			sp2=s2; st2=sw2; t2=mt2;
			m2=document.getElementById(preview_slider);
			sa2=m2.getElementsByTagName('li');
			l2=sa2.length; w2=m2.offsetWidth; sw2=(w2/l2);
			ot2=Math.floor((w2-st2)/(l2-1)+6); var i=0;
			
			if (!isInit) {
				//console.log(sw2);
				//console.log('---------');
				for (var i = 0; i<l2; i++){
					s2=sa2[i];
					if (i == (sl2-1)) {
						s2.style.width=sw2 + 'px';
						
						/*var cwidth = 0;
						cwidth = 900; //940 - (940/(l2-1))*100;
						s2.style.width = cwidth + 'px';*/
					}
					else {						
						var cwidth = 0;
						
						cwidth = Math.floor((940-sw2)/(l2-1));
						//console.log(cwidth);
						s2.style.width = cwidth + 'px';
						//s2.style.width = '100px';
					}
					this.timer(s2);
				}
				//if(sl2!=null){m2.timer=setInterval(function(){slideMenu2.slide(sa2[sl2-1])},t2)}
			}
			else {
				for(i;i<l2;i++){
					s2=sa2[i];
					s2.style.width=sw2+'px';
					this.timer(s2)
				}
				if(sl2!=null){
					m2.timer=setInterval(function(){
						prevCurrent = sa2[sl2-1];
						currentBgPos = jQuery('div.main', sa2[sl2-1]).css('backgroundPosition');
						slideMenu2.slide(sa2[sl2-1])
					},t2)
					
				}
			}
		},
		timer:function(s2){
			s2.onmouseover=function(){
				// check first and last image
				if(s2 != prevCurrent) {
					//reset previous current element
					jQuery('div.main', prevCurrent).css('backgroundPosition', currentBgPos);
					//new current bg pos
					currentBgPos = jQuery('div.main', s2).css('backgroundPosition');
					prevCurrent = s2;
				}
				clearInterval(m2.timer);
				m2.timer=setInterval(function(){
					slideMenu2.slide(s2)
				},t2)
				
			}
		},
		slide:function(s2){
			
			if(s2 == sa2[1]) {
				jQuery('div.main', s2).css('backgroundPosition', '0 0');
			} else {
				jQuery('div.main', s2).css('backgroundPosition', '0 0');
				jQuery('div.main', sa2[1]).css({'backgroundPosition' : '-' + pos + 'pt 0pt'});
			}
			
			var cw2=parseInt(s2.style.width,'10');
			if(cw2<st2){
				var owt2=0; var i=0;
				for(i;i<l2;i++){
					if(sa2[i]!=s2){
						var o2,ow2;
						var oi2=0;
						o2=sa2[i];
						ow2=parseInt(o2.style.width,'10');
						if(ow2>ot2){
							oi2=Math.floor((ow2-ot2)/sp2);
							oi2=(oi2>0)?oi2:1;
							o2.style.width=(ow2-oi2)+'px'
						}
						owt2=owt2+(ow2-oi2)
					}
				}
				var except_li = l2-1;
				extra_space = except_li * 6; // to nutalize the margin padding
				s2.style.width=(w2-owt2)+extra_space+'px'; // total output value
				
				//s2.style.width=(w2-owt2)+'px'; // origional code.
			}else{clearInterval(m2.timer)}
		}
	};
}();

