$(document).ready(function(){
	jQuery.fn.openmenu = function(){
		menu = $(this);
		li = menu.find('li');
		
		var parentitems = function(){
			items = [];li.each(function(n){if(li.eq(n).find('ul').length > 0)items.push(li.eq(n));});return items;
		}
		
		var openmenuContent = {
			parentItems: parentitems(),
			timeslide: 600
		}, c = openmenuContent;
		
		var click = function(){
			ul = $(this).parent().children('ul');
			if(ul.css('display') == 'block') ul.animate({height:'hide', opacity:'hide'}, c.timeslide);
				else ul.animate({height:'show', opacity:'show'}, c.timeslide);
			return false;
		}
		
		for(i=0;i<c.parentItems.length;i++){
			c.parentItems[i].find('a').eq(0).bind('click', click);
		}
	}
	$('.smenu').openmenu();
	
	jQuery.fn.openphoto = function(){
		links = $(this).find('.pitem a');
		
		var hide = function(){
			$('body').append('<div id="hide" style="width:'+$(document).width()+'px;height:'+$(document).height()+'px"></div>');
		}
		var openimg = function(src, alt){
			if(src){
				hide();
				$('body').append('<div class="singleimg"><img src="'+src+'" alt="'+alt+'"/>'+alt+'</div>');
				img = $('.singleimg img');
				img.parent().css({
					marginLeft:($(document).width()-img.width())/2,
					marginTop:($(window).height()-img.height())/2-100
				});
			}
		}
		var clicked = function(){
			link = $(this);
			openimg(link.attr('href'), link.children('img').attr('alt'));
			return false;
		}
		var close = function(){
			$('#hide').remove();
			$('.singleimg').remove();
		}
		
		links.bind('click', clicked);
		$('#hide').live('click', close);
	}
	$('.photos').openphoto();
	
	jQuery.fn.newprods = function(options){
		var defaults = {
 			visibleCount: 1,
			next: $('.newprods .next a'),
			prev: $('.newprods .prev a'),
  		}, params = $.extend(defaults, options);
		
		ul = $(this);
		li = ul.find('li');
		w = li.width();
		c = li.length;
		
		$next = params.next;
		$prev = params.prev;
		
		ul.wrap('<div class="newprods-clip"></div>');
		ul.width(w*c);
		li.width(w).css({'float':'left'});
		
		time = 500;
		
		var checknext = function(){
			if(parseFloat(ul.css('margin-left')) <= -w*(c-1))
				ul.animate({marginLeft:'-'+w*(c-1)+'px'}, time);
		}
		var checkprev = function(){
			if(parseFloat(ul.css('margin-left')) >= 0)
				ul.animate({marginLeft:0}, time);
		}
		var next = function(){
			if(parseFloat(ul.css('margin-left')) > -w*(c-1))
				ul.animate({marginLeft: '-='+w+'px'}, time, checknext);
			return false;
		}
		var prev = function(){
			if(parseFloat(ul.css('margin-left')) < 0)
				ul.animate({marginLeft: '+='+w+'px'}, time, checkprev);
			return false;
		}
		
		$next.bind('click', next);
		$prev.bind('click', prev);
	}
	$('.newprods ul').newprods();
});
