function showgoods(id, title) {
	var dd = $('<div>').addClass('dialog');
	dd.dialog({
		autoOpen: true,
		position: "center",
		close: function (event, ui) {
			$(this).dialog('destroy');
			dd.remove();
		},
		open: function () {
			$(this).append($('<img>').attr({
				src: '/css/images/ui-anim_basic_16x16.gif'
			}));
			if (!$('body').data("d"+id)) {
				$.get("/show.php", {
					'id': id,
					'short': 'true'
				}, function (data) {
					$('body').data("d"+id, data);
					dd.html(data);
					new ProductPresenter({
						'dialogBox': dd,
						'id': id
					});
				});
			} else {
				dd.html($('body').data("d"+id));
			}
		},
		buttons: {
		},
		width: 770,
		height: 650,
		title: title
	});
}
function ProductPresenter(options) {
	var self = this;
	this.options = options;
	this.init = function() {
		
	}
	this.toBasket = function() {
		$.post('/ajax.php', {
			'id': self.options.id,
			'action': 'toBasket'
		}, function(response) {
			if (response.status == 'OK') {
				$('.basket-short').html(response.basketShort);
			} else {
				alert(response.message);
			}
		}, 'json');
	}
	this.init();
}

function BasketPresenter(options) {
	var self = this;
	this.options = options;
	this.init = function() {
		$('.basket-short .product .delete').live('click', self.del);
		$('.basket-short .product .name').live('click', self.showProduct);
	}
	this.del = function() {
		var id = this.id.replace('del-', '');
		$.post('/ajax.php', {
			'id': id,
			'action': 'delete'
		}, function(response) {
			if (response.status == 'OK') {
				$('.basket-short').html(response.basketShort);
			} else {
				alert(response.message);
			}
		}, 'json');
	}
	this.showProduct = function () {
		var id = this.id.replace('product-', '');
		showgoods(id, $(this).find('.name').text());
	}
	this.init();
}

function bigphoto(obj) {
	var str=obj.src.toString()
	document.getElementById('mainphoto').src=str.replace('/1/', '/3/')
}
function f(id, und) {
	var urls = ['nshpp', 'violant', 'td_violant'];
	id = id || 1;
	if (urls[id - 1] != und) {
		document.location = urls[id - 1];
	}
}



function CmenuPresenter() {
	var self = this;
	this.init = function() {
		$(".cmenu a").live('mouseover', function () {
			var img = $(this).find('img');
			img.attr('src', img.attr('src').replace("color=bd291c", "color=fcf982"));
		}).live('mouseout', function () {
			var img = $(this).find('img');
			if (!$(this).hasClass('selected')) {
				img.attr('src', img.attr('src').replace("color=fcf982", "color=bd291c"));
			}
		});
	}

	this.init();
}

CmenuPresenter.openCloseMenuItem = function (el) {
	var li = $(el).parent();
	if (li.find('ul').size() <= 0) {
		return true;
	}
	li.find('ul:first').toggleClass('hidden', 1000);
	return false;
}

RubricsPresenter = function () {
	this.init = function() {
		$(".rubrics ul li a").bind('mouseover', function () {
			var img = $(this).find('img');
			img.attr('src', img.attr('src').replace("color=fff3e4", "color=5e0303"));
		}).bind('mouseout', function () {
			var img = $(this).find('img');
			img.attr('src', img.attr('src').replace("color=5e0303", "color=fff3e4"));
		});
	}
	this.init();
}

NewsPresenter = function () {
	var col1 = "bd291c", col2 = "fcf982";
	this.init = function() {
		$("#right .news h1 a").bind('mouseover', function () {
			var img = $(this).find('img');
			img.attr('src', img.attr('src').replace("color=" + col1, "color=" + col2));
		}).bind('mouseout', function () {
			var img = $(this).find('img');
			img.attr('src', img.attr('src').replace("color=" + col2, "color=" + col1));
		});
	}
	this.init();
}


$(document).ready(function () {
	new BasketPresenter;
	new CmenuPresenter();
	// menu fix for ie6
	if ( $.browser.msie && $.browser.version <= 6) {
		$('.mmenu li').bind('mouseover', function () {
			$(this).addClass('over');
		}).bind('mouseout', function() {
			$(this).removeClass('over');
		});
	}
	/*$(".round").curvyCorners({
		tl: { radius: 5 },
		tr: { radius: 5 },
		bl: { radius: 5 },
		br: { radius: 5 },
		antiAlias: true
	});*/
});


jQuery.fn.curvyCorners = function (settings) {
	var curvy = function () { curvyCorners(settings, this); }
	if ($.browser.opera && $.browser.version.replace(/(\d+).*/, "$1") >= 10) {
		curvy = function () { $(this).css('border-radius', '5px'); }
	}
	return this.each(function () {
		curvy.call(this);
	});
}

var counter = 0;
jQuery.fn.flash = function (src, options) {
	var settings = options || {};
	return this.each(function () {
		if (!this.id) {
			this.id = "flash-" + counter++;
		}
		var id = this.id;
		var attributes = {
			base: settings.base || '',
			bgcolor: settings.bgcolor || "#000"
		};
		var params = {
			base: settings.base || '',
			bgcolor: settings.bgcolor || "#000",
			menu: settings.menu || "true",
			scale: settings.scale || "noscale",
			allowFullScreen: settings.allowFullScreen || "true"
		}
		if (settings.wmode) {
			params.wmode = settings.wmode;
		}
		var width = settings.width || "100%";
		var height = settings.height || "100%";
		var ver = settings.version || '9.0.0';
		var install = settings.installScript || "/js/expressInstall.swf";
		var flashVars = settings.flashVars || {};
		if (!swfobject.hasFlashPlayerVersion(ver)) {
			$(this).show();
		}
		swfobject.embedSWF(src, id, width, height, ver, install, flashVars, params, attributes);
	});
}


function navigateTo(to) {
	var urls = ['', 'violant', 'td_violant'];
	$.address.update(to);
}
