var image = {
	
	isImage: function(href) {
		point = href.lastIndexOf('.');
		href = href.substring(point+1, href.length);
		if (href=='jpg' || href=='jpeg' || href=='bmp' || href=='png' || href=='gif' || href=='tiff') return true;
		return false;
	}
	
}

function getBrowserInfo() {
    var t,v = undefined;
   
    if (window.chrome) t = 'Chrome';
    else if (window.opera) t = 'Opera';
    else if (document.all) {
        t = 'IE';
        var nv = navigator.appVersion;
        var s = nv.indexOf('MSIE')+5;
        v = nv.substring(s,s+1);
    }
    else if (navigator.appName) t = 'Netscape';
   
    return {type:t,version:v};
}

function bookmark(a){
    var url = window.document.location;
    var title = window.document.title;
    var b = getBrowserInfo();
   
    if (b.type == 'IE' && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title);
    else if (b.type == 'Opera') {
        a.href = url;
        a.rel = "sidebar";
        a.title = url+','+title;
        return true;
    }
    else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
    else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
    return false;
} 


/**
 * Calculator
 */
Calculator = function(s) {
	this.init(s);
}

Calculator.prototype = {
	s: null,
	price: 0,
	
	init: function(s) {
		this.s = s;
		this.price = parseInt(s.find('.price').html());
		this.attachEvents();
		this.recalc();
	},

	recalc: function() {
		var count = parseInt(this.s.find('.count').val());
		var total = count * this.price;
		
		this.s.find('.total').html(total);
	},
	
	inc: function() {
		var count = parseInt(this.s.find('.count').val());
		count++;
		this.s.find('.count').val(count);
		
		this.recalc();
	},
	
	dec: function() {
		var count = parseInt(this.s.find('.count').val());
		count--;
		if (count<1) {
			count = 1;
		}
		this.s.find('.count').val(count);
		
		this.recalc();
	},
	
	attachEvents: function() {
		var that = this;
		this.s.find('.count').change(function(){
			that.recalc();
		});
		this.s.find('input.plus').click(function(){
			that.inc();
		});
		this.s.find('input.minus').click(function(){
			that.dec();
		});
	}
}

/**
 * Slideshow
 */
Slideshow = function(files) {
	this.init(files);
}

Slideshow.prototype = {
	TIMEOUT: 4000,
	HIDETIMOUT: 700,
	
	files: null,
	index: 0, prev: null,
	img: null,
	baseImg: null,
	
	init: function(files) {
		this.files = files;
		this.baseImg = (jQuery)(document.createElement('img'))
					.appendTo('.splash')
					.css('position', 'absolute')
					.hide();
		this.img = (jQuery)(document.createElement('img'))
					.appendTo('.splash');
		this.showNext();
	},
	
	showNext: function() {
		if (this.prev!=null) {
			this.baseImg.attr('src', this.files[this.prev])
						.show()
						.fadeOut(this.HIDETIMOUT);
			
		}
		this.img.attr('src', this.files[this.index]);
		this.prev = this.index; 
		this.index++;
		if (this.index>=this.files.length) {
			this.index = 0;
		}
		var that = this;
		setTimeout(function(){
			that.showNext();
		}, this.TIMEOUT);
	}
}

// doc

$(document).ready(function(){
	if ($('.splash').length>0) {
		if (slideshowImages.length>0) {
			new Slideshow(slideshowImages);			
		} else {
			$('.splash').css('margin-top', '-1px');
		}
		
	}
	if ($('.basket').length>0) {
		$('.basket').each(function(){
			new Calculator($(this));
		});	
	}
	if ($('.menu.sidebar').length>0) {
/*		$('.menu.sidebar .have-sub').each(function(){
			$(this).click(function(){
				$(this).toggleClass('expanded');
				$(this).next().toggle();
				return false;
			});
		});*/
	}
	if ($('.wysiwyg-content').length>0) {
		$('.wysiwyg-content table').each(function(){
			$(this).find('tr:odd').each(function(){
				$(this).addClass('odd');
			});
		});
	}
	
	$("a[target=_blank]").each(function(){
		var href = $(this).attr("href");
		if (image.isImage(href)) {
			$(this).lightBox();
		}
	});
	$('a[rel=lightbox]').each(function(){
		$(this).lightBox();
	});;
});

