var $j = jQuery.noConflict();

////////////////////
function marquee(){
	$j('.zmarquee').each(function(){
		var jmarquee = $j(this),
			jwrap = jmarquee.find('ul:first'),
			jitems = jwrap.children(),
			displaywidth = jmarquee.outerWidth(true),
			itemwidth = jitems.eq(0).outerWidth(true),
			nvisible = Math.ceil(displaywidth / itemwidth),
			nitems = jitems.length,
			totalwidth = 0,
			ndups = Math.round(nvisible + 3);
		
		for(var i = 0; i < nitems; i++){
			totalwidth += jitems.eq(i).outerWidth(true);
		}
		
		for(var i = 0; i < ndups; i++){
			jitems.eq(i).clone(true).appendTo(jwrap);
		}
		
		var marginleft = 0,
			run = function(){
				marginleft++;
				if(marginleft >= totalwidth){
					marginleft = 0;
				}
				
				jwrap.css('marginLeft', -marginleft);
			},
			runid = null;
			
		runid = setInterval(run, 17);
		
		jmarquee.mouseenter(function(){
			clearInterval(runid);
		}).mouseleave(function(){
			clearInterval(runid);
			runid = setInterval(run, 17);
		});
	});
	
	$j.fn.zshowtip = function(options){

        var defaults = {
			tooltip: '.tooltip',
			showat: '.view-tooltip',
			offset: {
				x: 0,
				y: 0
			}
		};

        options = $j.extend(defaults, options);
		
		var reposition = function(jtip, offset, e){
			var width = jtip.outerWidth(true),
				height = jtip.outerHeight(true),
				left = e.pageX + offset.left,
				top = e.pageY + offset.top;
				
		
			left = (left + width> $j(document).scrollLeft() + $j(window).width())? left - width - (offset.left * 2) : left;
			top = (top + height > $j(document).scrollTop() + $j(window).height())? $j(document).scrollTop() + $j(window).height() - height - offset.top : top;
		
			jtip.css({
				top: top,
				left: left
			});
		};

		return this.each(function(){
			var jcontainer = $j(this),
				jtooltip = jcontainer.find(options.tooltip),
				jshowat = jcontainer.find(options.showat);
				
			if(!jtooltip.length || jtooltip.length != jshowat.length){
				return;
			}
			
			//var offset = jshowat.offset();
			var offset = {
				left: 20,
				top: 20
			};
			
			jshowat.mouseenter(function(e){
				reposition(jtooltip, offset, e);
				
				jtooltip.stop().css({
					opacity: 0//,
					//top: jshowat.offset().top - jtooltip.height() + options.offset.y,
					//left: Math.min($j(window).width(), jshowat.offset().left + (jshowat.width() - jtooltip.width()) / 2 + options.offset.x)
				}).fadeTo(500, 1, function(){
					$j(this).css('filter', '');
				});
			}).mousemove(function(e){
				reposition(jtooltip, offset, e);
			}).mouseleave(function(){
				jtooltip.stop().fadeTo(250, 0, function(){
					jtooltip.css('top', -70000);
				});
			});
			
			jtooltip.removeClass('hidden').css('top', -70000).appendTo(document.body);
		});	
    };
	
	
}

/////////////////////
/*****************/
function zhorzgalleryfadenavauto(gal, info){
	
	var jelm = $j(gal);
	
	if(!jelm.length){
		return;
	}
	
	var jitems = jelm.find(info.items),
		jthumbitems = jelm.find(info.thumbitems);
		
	if(jthumbitems.length < 1){
		return;
	}
	
	var curitem = -1,
		timeoutid = null;
	
	jthumbitems.each(function(idx){
		$j(this).bind('click.zhgs', function(){
			if(curitem == idx){
				return false;
			}
			
			clearTimeout(timeoutid);
			
			jthumbitems.eq(curitem).removeClass('current');
			jitems.eq(curitem).stop(true).css('z-index', 0).fadeTo(800, 0);
			curitem = idx;
			jthumbitems.eq(curitem).addClass('current');
			jitems.eq(curitem).stop(true).css('z-index', 1).fadeTo(1200, 1);
			
			timeoutid = setInterval(next, 5000);
			
			return false;
		});
	});
	
	function next(){
		var nextitem = curitem + 1;
		if(nextitem > jitems.length -1){
			nextitem = 0;
		}
		
		jthumbitems.eq(nextitem).triggerHandler('click.zhgs');
	}
	
	for(var i = 0; i < jthumbitems.length; i++){
		if(jthumbitems.eq(i).hasClass('current')){
			jthumbitems.eq(i).triggerHandler('click.zhgs');
		}else{
			jitems.eq(i).css('opacity', 0);
		}
	};
}

function initgallery(){
	
	$j('#slideshow').each(function(){
		zhorzgalleryfadenavauto(this, {items: '.thumb', thumbitems: '.list-number ul:first li'});
	});	
	
}


////////////////////
(function($j){

   	$j.fn.ztab = function(options){

        var defaults = {
			head: '.jhead',
			content: '.jcontent',
			onShow: null
		};

        options = $j.extend(defaults, options);

		return this.each(function(){
			var jcontainer = $j(this),
				jheads = jcontainer.find(options.head),
				jcontents = jcontainer.find(options.content);
				
			if(!jheads.length || jheads.length != jcontents.length){
				return;
			}
			
			var headidx = 0;
			
			jheads.each(function(idx){
				if($j(this).hasClass('current')){
					headidx = idx;
				}
				
				$j(this).click(function(){
					if(headidx != idx){
						jheads.eq(headidx).removeClass('current');

						jcontents.eq(headidx).addClass('hidden');
						
						$j(this).addClass('current');
						
						if($j.browser.msie && parseInt($j.browser.version) < 9){
							jcontents.eq(idx).removeClass('hidden');
							if(options.onShow){
								options.onShow.call(jcontents.eq(idx));
							}
						}else{
							jcontents.eq(idx).removeClass('hidden').css('opacity', 0).fadeTo(500, 1, options.onShow);
						}
						
						headidx = idx;
					}
					
					return false;
				});
			});
		});	
    };

		
	$j.fn.showPopup = function(options){
		var defaults = {
			zIndex: 980,
			opacity: 0.5,
			closeButtonClass: ['close'],
			overlayDuration: 250,
			popupDuration: 500,
			autoClose: false,
			noOverlay: false,
			confirmPopup: false,
			confirmOK: '',
			destroyOnHide: false,
			dockPanel: false, //1 = top left, 2 = top right, 3 = bottom left, 4 = bottom right, default = center
			offset: {
				x: 0,
				y: 0
			},
			dockOffset: 15,			
			onShow: null,
			onHide: null,
			onConfirm: null,
			onClose: null
		};
		
		return this.each(function(){  
			var jpopup = $j(this),
				jwindow = $j(window),
				jhtml = $j('body'),
				initScrollTop = jwindow.scrollTop(),
				opt = $j.extend(defaults, options);
			
			if(!opt.noOverlay){
				var overlay = $j('<div></div>').css({
					'display': 'block',
					'visibility': 'visible',
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'width': Math.max(jwindow.width(), jhtml.innerWidth()),
					'height': Math.max(jwindow.height(), jhtml.innerHeight()),
					'zIndex': opt.zIndex,
					'backgroundColor': '#000',
					'opacity': 0
				}).appendTo(document.body);	
				
				jpopup.insertAfter(overlay);
			}	
			
			var rePosition = function(){
				var winWidth = jwindow.width(),
					winHeight = jwindow.height(),
					popupWidth = jpopup.width(),
					popupHeight = jpopup.height(),
					top = Math.max(0, (winHeight - popupHeight)/2),
					left = Math.max(0, (winWidth - popupWidth)/2);	
					
				if(opt.dockPanel){
					switch (opt.dockPanel){
						case 1: //top left
							top = left = opt.dockOffset;
						break;
						
						case 2: //top right
							top = opt.dockOffset;
							left = Math.max(0, winWidth - popupWidth - opt.dockOffset);
						break;
						
						case 3: //bottom left
							top = Math.max(0, winHeight - popupHeight - opt.dockOffset);
							left = opt.dockOffset;
						break;
						
						case 4: //bottom right
							top = Math.max(0, winHeight - popupHeight - opt.dockOffset);
							left = Math.max(0, winWidth - popupWidth - opt.dockOffset);
						break;
						
						default: //same as center
						break;
					}
				}
				
				return {top: top, left: left};
			}, windowScroll = function(){
				if(opt.el){
					var newpos = rePosition();					
					jpopup.css({
						'top': newpos.top,
						'left': newpos.left
					});
				}
				else{
					if(jwindow.height() < jpopup.outerHeight(true) || jwindow.width() < jpopup.outerWidth(true)){
						jpopup.css({
							'position': 'absolute',
							'top': initScrollTop
						});
					}else{
						if(jpopup.css('position') != 'fixed'){
							var newpos = rePosition();
							
							jpopup.css({
								'position': 'fixed',
								'top': newpos.top
							});
						}
					}
				}
			}, windowResize = function(){
				var newpos = rePosition();
				if(opt.el){
					jpopup.css({
						'top': newpos.top,
						'left': newpos.left
					});
				}
				else{
					jpopup.css({
						'position': ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) ? 'absolute' : 'fixed',
						'top': newpos.top,
						'left': newpos.left
					});	
				}
				
				if(overlay){
					overlay.css({	
						'width': jhtml.innerWidth(),
						'height': jhtml.innerHeight()
					});
				}
			};
			
			if(opt.dockPanel != 5){
				jwindow.bind('scroll.jpopupev', windowScroll);
				jwindow.bind('resize.jpopupev', windowResize);
			}
			
			var closeBtn = jpopup.find('.' + opt.closeButtonClass[0]);
			if(closeBtn.length){
				closeBtn.unbind('click.jpopupev').bind('click.jpopupev', function(e){
					
					if(opt.onHide){
						opt.onHidecall(jpopup);
					}
					
					if($j.browser.msie && parseFloat($j.browser.version) < 9){
						setTimeout(function(){
							if(opt.destroyOnHide){
								jpopup.remove();
							}else{
								jpopup.css('top', -70000);
							}
							
							if(opt.onClose){
								opt.onClose.call(jpopup);
							}
						}, opt.popupDuration / 3);
						
					}else{
						jpopup.stop(true).fadeTo(opt.popupDuration, 0, function(){
							if(opt.destroyOnHide){
								jpopup.remove();
							}else{
								jpopup.css('top', -70000);
							}
							
							if(opt.onClose){
								opt.onClose.call(jpopup);
							}
						});
					}
					
					if(overlay){
						overlay.stop(true).fadeTo(opt.overlayDuration, 0, function(){
							overlay.remove();
						});
					}
					
					jwindow.unbind('scroll.jpopupev', windowScroll);
					jwindow.unbind('resize.jpopupev', windowResize);
					
					return false;
				});
				
				if(opt.closeButtonClass.length > 1){
					jpopup.find('.' + opt.closeButtonClass.slice(1).join(',.')).click(function(){
						closeBtn.trigger('click');
						return false;
					});
				}
				
				if(overlay && !opt.confirmPopup){
					overlay.unbind('click.jpopupev').bind('click.jpopupev', function(e){
						closeBtn.trigger('click');
						
						return false;
					});
				}
			}
			
			var pos = rePosition(),
				position = ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) || opt.dockPanel == 5? 'absolute' : 'fixed';
			
			jpopup.css({
				'position': position,
				'top': pos.top + (position == 'fixed'? 0 : initScrollTop),
				'left': pos.left,
				'zIndex': opt.zIndex + 1
			});
			
			
			if(overlay){
				overlay.stop(true).fadeTo(opt.overlayDuration, opt.opacity);
			}
			
			if(opt.onShow){
				opt.onShow.call(jpopup);
			}
			
			if($j.browser.msie && parseFloat($j.browser.version) < 9){
				setTimeout(function(){
					jpopup.css('display', 'block');
				}, opt.popupDuration / 3);
			}else{
				jpopup.css('opacity', 0).stop(true).fadeTo(opt.popupDuration, 1);
			}
		});
	};
	
		
})(jQuery);

//////////////gaallery //////////////

///////////////////////

$j(document).ready(function(){
	marquee();
	$j('.highlight').ztab({head: '.listtabs li', content: '.highlight-inner .tabs'});
	initgallery();
	$j('.product-info2-inner2').zshowtip({offset: {x: 0, y: -10}});
	
});

 $j(window).load(function() {
	$j('#slideshow .nivoSlider').nivoSlider();
});
