/**
 * Shopping Main
 */

// ·Ñ·¯
var jvRollerControl = {

	interval: 3000,
		
	create: function(options, count) {
		var self = this;
		var roller = new jvRoller(
				count, 
				this.interval, 
				function(r) {
					var current = r.current();
					self.buttonOnOff(options.button, current);
					if (typeof options.pannel == "string") {
						self.panelOnOff(options.pannel, current);
					}
					else {
						options.pannel(current);
					}
				}
		);
		
		if (typeof options.pannel == "string") {
			jQuery(options.pannel).mouseover(function() {
				roller.stop();						
			}).mouseout(function() {
				roller.start();						
			}).click(function() {
				options.view(this.id.split("_")[2]);
			});
		}
		
		jQuery(options.button).click(function() {
			roller.current(this.id.split("_")[2]);
			var current = roller.current();
			self.buttonOnOff(options.button, current);
			if (typeof options.pannel == "string") {
				self.panelOnOff(options.pannel, current);
			}
			else {
				options.pannel(current);
			}
		});
		
		roller.start();
	},

	buttonOnOff: function(buttonTag, current) {
		jQuery(buttonTag).each(function(i, img) {
			if (i == current-1) {
				img.src = img.src.replace("_off", "_on");
			}
			else {
				img.src = img.src.replace("_on", "_off");
			}
		});
	},
	
	panelOnOff: function(panelTag, current) {
		jQuery(panelTag).hide();
		jQuery(panelTag + ":nth-child(" + current + ")").show();
	},

	nothing: function() {}
};

// ÂøÇÑ °¡°Ý
var jvProductDiv01_1 = {

	count: 0,
	links: new Array(),
	
	startup: function() {
		var self = this;
		
		jQuery("DIV.productDiv01 DIV.price01").load(
				"/shop/main/productDiv01_1.jsp",
				function() {
					jvRollerControl.create({
						button: "DIV.productDiv01 DIV.price01 H4.Title SPAN.tab IMG",
						pannel: "DIV.productDiv01 DIV.price01 DIV IMG",
						view: function(index) {
							self.show(index);
						}
					}, self.count);
				}
		)
	},
	
	show: function(index) {
		document.location = this.links[index];
	},
	nothing: function() {}
}

// »óÇ°Á¦¾È
var jvProductDiv01_2 = {

	count: 0,
	index: 1,
	products: new Array(),
	
	startup: function() {
		var self = this;
		
		jQuery("DIV.productDiv01 DIV.suggest").load(
				"/shop/main/productDiv01_2.jsp",
				function() {
					
					jQuery("#suggest_img").click(function() {
						self.open();
					});
					
					jvRollerControl.create({
						button: "DIV.productDiv01 DIV.suggest H4.Title SPAN.tab IMG",
						pannel: function(index) {
							self.show(index);
						}
					}, self.count);
				}
		)
	},
	
	show: function(index) {
		this.index = index || this.index;
		var info = this.products[index];
		if (info) {
			jQuery("#suggest_img").attr("src", info.image)
			//jQuery("#suggest_comment").html(info.comment);
			jQuery("#suggest_prdname_span").html("<a href=\"" +info.link+ "\" title=\"" +info.prdname+ "\">" +info.shortprdname+ "</a>");
			
			if (info.price1 == info.price2) {
				jQuery("#suggest_price1_span").empty();
			}
			else {
				jQuery("#suggest_price1_span").html(info.price1 + "¿ø");
			}
			jQuery("#suggest_price2_span").html(info.price2 + "¿ø");
		}
	},
	
	open: function() {
		//window.open(this.products[this.index].link);
		document.location = this.products[this.index].link;
	},
	
	nothing: function() {}
}

var jvShopping = {

	startup: function() {
		var self = this;
		
		jvProductDiv01_1.startup();
		jvProductDiv01_2.startup();
		
		jQuery("DIV.shoppingCast").load("/shop/main/shoppingCast.jsp", function() {
			jvShoppingCast.initialize();
		});
		jQuery("DIV.productDiv02").load("/shop/main/productDiv02.jsp");
		jQuery("DIV.productDiv03").load("/shop/main/productDiv03.jsp", function() {
			jQuery("DIV.productDiv03 .tabMenu LI SPAN").click(function() {
				self.toggleFavorite(this);
			});
			self.showFavorite(1);								
		});
		jQuery("#planningBanner").load("/shop/main/planningBanner.jsp");
		jQuery("DIV.productDiv04").load("/shop/main/productDiv04.jsp", function() {
			jQuery("DIV.productDiv04 DIV.categoryProduct .tabMenu LI IMG").mouseover(function() {
				self.toggleCategory(this);
			});
			self.showCategory(1);								
		});
		jQuery("DIV.mainEtc").load("/shop/main/notice.jsp");
	},
	
	toggleFavorite: function(t) {
		var tab = t.id.split("_")[1];
		
		jQuery("DIV.productDiv03 .tabMenu LI SPAN").each(function(index, element) {
			if (t == element) {
				jQuery(t).removeClass("off").addClass("on");
			}
			else {
				jQuery(element).removeClass("on").addClass("off");
			}
		})
		
		this.showFavorite(tab); 
	},
	
	showFavorite: function(tab) {
		jQuery('DIV.productDiv03 DIV.listBox').load(
			'/shop/main/favoriteShop.jsp',
			{
				tab: tab,
				ts: new Date().getTime()
			},
			function(data, textStatus) {
			},
			'html'
		);
	},
	
	toggleCategory: function(t) {
		var tab = t.id.split("_")[1];
		
		jQuery("DIV.productDiv04 DIV.categoryProduct .tabMenu LI IMG").each(function(index, element) {
			if (t == element) {
				element.src = element.src.replace("_off", "_on");
			}
			else {
				element.src = element.src.replace("_on", "_off");
			}
		})
		
		this.showCategory(tab); 
	},

	showCategory: function(tab) {
		jQuery('DIV.productDiv04 DIV.categoryProduct .listBox').load(
			'/shop/main/categoryShop.jsp',
			{
				tab: tab,
				ts: new Date().getTime()
			},
			function(data, textStatus) {
			},
			'html'
		);

		jQuery('DIV.productDiv04 .bestProduct').load(
			'/shop/main/categoryBest.jsp',
			{
				tab: tab,
				ts: new Date().getTime()
			},
			function(data, textStatus) {
			},
			'html'
		);
	}
}

