/*
 * 
 * 
 * 
 * 
 * 
 */

function stopDefault(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    else {
        window.event.returnValue = false;
    }
    return false;
}



var undf;
jQuery.fn.menuscrollline = function(options) {

	this.uid = "u" + parseInt(10000000 * Math.random());

	var objects = [];

	this.each(function(i) {
		var self = this;
		var gwidth = $(this).width();
		var scroll = 132;//$("li",self).width() + $("li",self).css("margin-left") +  $("li",self).css("margin-right");
		var coll = 1;
		var arrow_w = 25;
		this.page= options.page !== undf ? options.page : 0; 
		this.li = [];

		var pqty = $('li',this).length;
		var pqty_vis = Math.floor(gwidth / scroll);
		
	   this.pqty_vis = pqty_vis;
	   this.scroll = scroll;		
		objects[i] = this;
		$('ul',this).addClass('msl-ul');
		$('ul', this).wrapAll('<div class="msl-image-line"/>');
		var add_style = '';
		if(pqty > pqty_vis) add_style = 'margin:0;';
		$('ul', this).wrapAll('<div class="msl-ulwrapper" style="' + add_style + '"/>');
		$('.msl-ulwrapper', this).css('width',
				(scroll * $('li', this).length) + (gwidth > 800?7:9)); //mega dirty
		$('.msl-image-line', this).wrapAll('<div class="msl-image-line-content" />');
		$('.msl-image-line-content', this).wrapAll('<div class="msl-head" />');
		
		if(pqty > pqty_vis){
   		$('.msl-image-line', this).before(
   				'<div class="msl-scroll msl-scroll-left"></div>');
   		$('.msl-image-line', this).after(
   				'<div class="msl-scroll msl-scroll-right"></div>');
		}
		
   	$('.msl-image-line', this).after('<div class="msl-clear"></div>');
		
		//check width
      var pwidth = $('.msl-head').parent().width();
      if(typeof pwidth != 'number'){ pwidth = 700; }
         else{pwidth -= arrow_w * 2;}
   	$('.msl-image-line').css({'width': pwidth+'px', 'margin-left': arrow_w+'px', 'margin-right': arrow_w + 'px'});
   	
   	
		$('li',self).each(function(){
			var url = $('a',this).attr('href');
			$('img',this).wrap("<a href='"+url+"' />");
		});
		
		// $('.msl-head',this).after('<div class="msl-image-box"><img
		// class="msl-img"></div>');
		// $('.msl-head',this).after('<div class="msl-title" />');
		// $('.msl-image-box',this).after('<div class="msl-description"
		// />');

			$(document).keydown(function(e) {
					if (e.keyCode == 37) {
						self.prevPage();
					}

					if (e.keyCode == 39) {
						self.nextPage();
					}
				});

			/*
			 * $('.msl-ulwrapper',self).mousewheel(function(event, delta) {
			 * //console.log(delta); event.stopPropagation(); return false; });
			 */
			this.cc = 0;
			/*
			$('.msl-image-line', self).mousewheel(function(event, delta) {

				// console.log(event);
					event.stopPropagation();

					if (0 && !jQuery.browser.msie) {
						if (self.cc == 0) {
							self.cc = 1;
						} else {
							self.cc = 0;
							return false;
						}
					}
					if (delta > 0) {
						self.prevPage();
					}

					if (delta < 0) {
						self.nextPage();
					}
					return false;
				});
         */
			if(pqty > pqty_vis){
   			$('.msl-scroll-right', this).hover(function() {
   				if (!$(this).hasClass('msl-scroll-disabled')) {
   					$(this).addClass('msl-scroll-hover');
   				}
   			}, function() {
   				$(this).removeClass('msl-scroll-hover');
   			});
   			
   			$('.msl-scroll-left', this).hover(function() {
   				if (!$(this).hasClass('msl-scroll-disabled')) {
   					$(this).addClass('msl-scroll-hover');
   				}
   			}, function() {
   				$(this).removeClass('msl-scroll-hover');
   			});
			}

			$('li', this).hover(function(e) {
				//self.makeHover($(this));
			}, function(e) {
				//self.makeUnHover($(this));
			});

			this.makeScroll = function(q, byLimit) {
				if (q == 0)return;
				if (q < 0 && $(".msl-image-line", self).scrollLeft() - scroll < 0) return false;
				if (q > 0 && $(".msl-image-line", self).scrollLeft() + scroll > $(".msl-ulwrapper", self).width() - $(".msl-image-line", self).width()) return false;
				
				$(".msl-image-line", self).scrollLeft($(".msl-image-line", self).scrollLeft() + (scroll * q));
			}
			
			this.countPages = function(){

				return Math.ceil($("li", self).length / coll);
			}
			
			this.nextPage = function(){
				self.setPage(self.page+1, true);
				return self.page;
			}
			
			this.setPage = function(page,isanime){
				if (page < 0) return false;
				if (page > ( self.countPages() - this.pqty_vis) ) return false;	
				self.page = page;
				
				isanime = typeof(isanime) == 'undefined' ? false:isanime;
				
				var o = 0;
				var no = 0;
				
				o = scroll * page * (coll);

				var mw = scroll * $('li',self).length;
				
				/*
				if ( (no = mw - o) <  $('.msl-image-line',self).width()){
					o = $('.msl-image-line',self).scrollLeft() + no - scroll;
				}*/

				
				
				if(isanime){
				   $(".msl-image-line", self).animate({ scrollLeft: o }, "fast",
                  function(){self.checkScrolls();});
				}else{
				   $(".msl-image-line", self).scrollLeft(o);
				   self.checkScrolls(); 
				}
			}
			
			this.prevPage = function(){
				self.setPage(self.page - 1, true);
			
			}

			this.needScroll = function() {
/*				 
	          var p = $(self.active).position(); 
				 var sl = $(".msl-image-line",self).scrollLeft(); 
				 var l = p.left; 
				 var o=44;
	          var mw = $('.msl-image-line',self).width() + o -1; 
	          var nw = o - 1;
	          var s = 0; 
	          
	          if (l < nw){s = (parseInt((l - nw - scroll)/ scroll));}
	          if (l > mw){s = (parseInt((l - mw + scroll) / scroll));}
	           
	          return s; 
*/			 
			}

			this.aNext = function() {
				var n = null;
				n = self.getNext()
				if (n.length) {
					// self.setActive(n);
				}
			}

			this.aPrev = function() {
				var n = null;
				n = self.getPrev()
				if (n.length) {
					// self.setActive(n);
				}
			}

			this.makeHover = function(obj) {
				if ($(obj).hasClass('msl-image-activ')) return;
				$('img', obj)
						.attr(
								'src',
								$('img', obj).attr('src').replace('default',
										'colored'));
			}

			this.makeUnHover = function(obj) {
				if ($(obj).hasClass('msl-image-activ')) return;
				$('img', obj)
						.attr(
								'src',
								$('img', obj).attr('src').replace('colored',
										'default'));
			}

			this.setActiveByNum = function(index) {
				if (self.li.length == 0) {
					$("li", self).each(function(i) {
						self.li[i] = this;
					});
				}

				self.setActive(self.li[index]);

			}
			
			this.getActiveNum = function() {
			   return jQuery('ul.menuscrollline li').index(jQuery('#current')) + 1; 
			}
			
			this.getBlocksQty = function() {
			   return jQuery('ul.menuscrollline li').length;
			}

			
			this.checkScrolls = function() {
				$(".msl-scroll", self).removeClass('msl-scroll-disabled');

				if (self.page == 0) {
					$(".msl-scroll-left", self).removeClass('msl-scroll-hover');
					$(".msl-scroll-left", self).addClass('msl-scroll-disabled');
				}

				if (self.page + self.pqty_vis >= self.countPages()) {
					$(".msl-scroll-right", self).removeClass('msl-scroll-hover');
					$(".msl-scroll-right", self).addClass('msl-scroll-disabled');
				}

			}
			
			
			
	
			this.setActive = function(obj) {

				self.active = obj;
/*
				var Iurl = $("a", obj).attr('href');
				var title = $("a", obj).attr('title');
				var desc = $("img", obj).attr('title');

				self.makeScroll(self.needScroll());
*/
	
				$('.msl-image-line li', self).removeClass('msl-image-activ');
				
				var ccv = $('img',obj).attr('src');	
				if (ccv){
				$('img',obj).attr('src',ccv.replace('default','colored'));
				}
				$(obj).addClass("msl-image-activ");


				self.checkScrolls();
				//self.savePage();
				
				// return false;
			}

			//this.savePage = function(){
				//$.cookies.set(hex_md5(location.href,self.page)+"_page", self.page);
			//}


			this.getNext = function() {
				return $(self.active, self).next();
			}

			this.getPrev = function() {
				return $(self.active, self).prev();
			}

			var images = [];

			$("li", this).each(function(i) {
				images[i] = {
					img : $("a", this).attr('href'),
					title : $("a", this).attr('title'),
					thumb : $("img", this).attr('src')
				};

				$(this).click(function(event) {
					if ($(this).attr('id') == 'current' && $(this).parent().hasClass('menuscrollline')) {
						return false;
					} else {
						window.location = $('a',this).eq(0).attr('href');
					}
					//self.setActive(this);
				});
				
			});

			$(".msl-scroll-left", this).click(function(e){self.prevPage();});
			$(".msl-scroll-right", this).click(function(e){self.nextPage();});

			//if (p = $.cookies.get(hex_md5(location.href)+"_page")){
			//	self.setPage(p);
			//}
			
			if (options.activeClass != null) {
				this.setActive($(".".options.activeClass, self));
			}

			if (options.active != null) {
				this.setActive($(options.active));
			}
			
			if(pqty > pqty_vis)
			   self.checkScrolls();
			

		});

	return objects;

};

jQuery(function(){
   $('ul.menuscrollline #current a').click(function() {
	   return false;
   });
})
