function popWin(url,win,para) {
    var win = window.open(url,win,para);
    win.focus();
}

function setLocation(url){
    window.location.href = url;
}

jQuery(document).ready(function() {
	toute.init();
});

function setPLocation(url, setFocus){
    if( setFocus ) {
        window.opener.focus();
    }
    window.opener.location.href = url;
}
jQuery(window).load(function() {
	jQuery('.main-menu .content a').each(function() {
		if (jQuery(this).height()<22) { jQuery(this).parent('.content').addClass("rem"); }
	});
	jQuery('.main-menu .act .content a').click(function(){
		return false;
	});
});

var toute = {
	init: function() {
		this.handleLogo();
		this.handleMainMenu();
		this.handleLeftMenu();
		this.handleLinks();
		this.handleTextFields();
		this.handleProdCols();
		this.handleListView();
		this.handleScroller();
		this.handleFakeSelect(baseUrl);
		this.handleCatBlock();
		this.handleCrawler();
		this.handleMemoryFront();
		this.handleMisc();
		this.handleSidebarPaging();
		this.handleTabs();
	},

	handleLogo: function() {
		jQuery('.site-logo a').hover(
			function() {
				jQuery('.site-logo-hover').css("visibility","visible");
			},
			function() {
				jQuery('.site-logo-hover').css("visibility","hidden");
			}
		);
	},

	handleMainMenu: function() {
		jQuery('.main-menu td:not(.act)').hover(
			function() {
				jQuery(this).addClass('on');
			},
			function() {
				jQuery(this).removeClass('on');
			}
		).click(function() {
			if (jQuery('a',this).attr("href")) { location.href = jQuery('a',this).attr("href"); }
		});
	},

	handleLeftMenu: function() {
		jQuery('.leftmenu a').click(function(e) { e.stopPropagation(); });
		jQuery('.leftmenu li').click(function(e) {
			e.stopPropagation();
			if (jQuery(this).hasClass("has-sub")) {
				jQuery(this).next("li").toggle();
				jQuery(this).toggleClass("on");
			} else {
				location.href = jQuery('a',this).attr("href");
			}
		});
	},

	handleLinks: function() {
		jQuery('a.submit-form').click(function(e) {
			e.preventDefault();

			jQuery('form#'+jQuery(this).attr("rel")).submit();
		});
	},

	handleTextFields: function() {
		jQuery('.focused-replace').focus(function() {
			if (jQuery(this).val()==jQuery(this).attr("title")) {
				jQuery(this).val("");
			}
		}).blur(function() {
			if (!jQuery(this).val()) {
				jQuery(this).val(jQuery(this).attr("title"));
			}
		});
	},

	handleProdCols: function() {
		jQuery('.prodcol').hover(
			function() {
				jQuery(this).addClass("on");
			},
			function() {
				jQuery(this).removeClass("on");
			}
		);

		var lock = 0;
		var timer = null;

		jQuery('.prodcol-switcher').parents(".prodcols").hover(
			function() {
				lock = 1;
			},
			function() {
				lock = 0;
			}
		);

		var funcSwitch = function() {
			if (!lock) {
				jQuery('.prodcol-switcher').each(function() {
					if (jQuery(this).children().length>1) {
						current_el = jQuery(this).children(".prodcol:visible");
						next_el = current_el.next(".prodcol");

						if (!next_el.length) {
							next_el = jQuery(this).children(".prodcol:first");
						}

						current_el.fadeTo(600,0,function() { jQuery(this).hide(); });
						next_el.css("opacity",0).show().fadeTo(600,1);
					}
				});
				
				timer = setTimeout(funcSwitch,5000);
			} else {
				timer = setTimeout(funcSwitch,2000);
			}
		}

		timer = setTimeout(funcSwitch, 5000);
	},

	handleListView: function() {
		jQuery('.list-view tr.cell-row').hover(
			function() { jQuery(this).addClass("on"); },
			function() { jQuery(this).removeClass("on"); }
		);

		jQuery('.list-view a.image').hover(
			function() {
				img = jQuery(this).attr("rel");
				var pos = {
					x: Math.floor(jQuery(this).offset().left+jQuery(this).outerWidth()+10)+"px",
					y: Math.floor(jQuery(this).offset().top-65)+"px"
				}

				jQuery('#list-view-img img').removeAttr("width").removeAttr("height").load(function() {
					dim = {
						w: jQuery(this).width()+"px",
						h: jQuery(this).height()+"px"
					}
					jQuery('#list-view-img').css({top:pos.y,left:pos.x}).show();
					jQuery('#list-view-img').css({width:dim.w,height:dim.h});
				}).attr("src",img);
			},
			function() {
				jQuery('#list-view-img').hide();
			}
		);
	},

	handleScroller: function() {
		var timer = null;
		var autoscroll = 1;
		var autoscroll_interval = 3000;
		var autoscroll_lock = 0;

		jQuery('.prodscroller').each(function() {
			var ptr = 0;
			var prods = jQuery('.scroller-wrap',this).children(".scrollprod");
			var count = prods.length;
			var prodid = jQuery('.prodname',this);
			var id1, id2, id3, id4;

			// Init
			jQuery('.movel',this).click(function(e) { e.preventDefault(); movel(); });
			jQuery('.mover',this).click(function(e) { e.preventDefault(); mover(); });

			prods.eq(0).click(function(e) { e.preventDefault(); mover(); });
			jQuery('a, .order-area',prods.eq(0)).click(function(e) { e.stopPropagation(); });
			prods.eq(2).click(function(e) { e.preventDefault(); movel(); });
			jQuery('a, .order-area',prods.eq(2)).click(function(e) { e.stopPropagation(); });

			val = jQuery('.field-prodname',prods.eq(1)).val() ? jQuery('.field-prodname',prods.eq(1)).val() : '&nbsp;';
			prodid.html(val);

			jQuery('.autoscroll',this).click(function(e){e.preventDefault();movel();});

			// Funcs
			var movel = function(autoscroller) {
				if (count>3 && !prods.filter(":animated").length) {
					id1 = ptr;
					id2 = (ptr+1>=count) ? (count-(ptr+1))*(-1) : ptr+1;
					id3 = (ptr+2>=count) ? (count-(ptr+2))*(-1) : ptr+2;
					id4 = (ptr+3>=count) ? (count-(ptr+3))*(-1) : ptr+3;

					prods.eq(id1).animate({left:"-=212px"});

					prods.eq(id2).animate({left:"-=212px"});
					jQuery(prods.eq(id2)).click(function(e) { e.preventDefault(); mover(); });
					jQuery('a, .order-area',prods.eq(id2)).click(function(e) { e.stopPropagation(); });

					prods.eq(id3).animate({left:"-=212px"});
					jQuery(prods.eq(id3)).unbind('click');
					val = jQuery('.field-prodname',prods.eq(id3)).val() ? jQuery('.field-prodname',prods.eq(id3)).val() : '&nbsp;';
					prodid.html(val);

					prods.eq(id4).css("left","636px").animate({left:"-=212px"});
					jQuery(prods.eq(id4)).click(function(e) { e.preventDefault(); movel(); });
					jQuery('a, .order-area',prods.eq(id4)).click(function(e) { e.stopPropagation(); });
				
					ptr = (ptr+1>=count) ? 0 : ptr+1;
				}
			}

			var mover = function() {
				if (count>3 && !prods.filter(":animated").length) {
					id1 = (ptr-1<0) ? count-1 : ptr-1;
					id2 = ptr;
					id3 = (ptr+1>=count) ? (count-(ptr+1))*(-1) : ptr+1;
					id4 = (ptr+2>=count) ? (count-(ptr+2))*(-1) : ptr+2;

					prods.eq(id1).css("left","-212px").animate({left:"+=212px"});
					jQuery(prods.eq(id1)).click(function(e) { e.preventDefault(); mover(); });
					jQuery('a, .order-area',prods.eq(id1)).click(function(e) { e.stopPropagation(); });

					prods.eq(id2).animate({left:"+=212px"});
					jQuery(prods.eq(id2)).unbind('click');
					val = jQuery('.field-prodname',prods.eq(id2)).val() ? jQuery('.field-prodname',prods.eq(id2)).val() : '&nbsp;';
					prodid.html(val);

					prods.eq(id3).animate({left:"+=212px"});
					jQuery(prods.eq(id3)).click(function(e) { e.preventDefault(); movel(); });
					jQuery('a, .order-area',prods.eq(id3)).click(function(e) { e.stopPropagation(); });

					prods.eq(id4).animate({left:"+=212px"});
				
					ptr = (ptr-1<0) ? count-1 : ptr-1;
				}
			}
		});

		var fautoscroll = function() {
			if (autoscroll && !autoscroll_lock) {
				jQuery('.prodscroller .autoscroll').click();
				jQuery('.movel').click();

			} else {
				//
			}
				
			timer = setTimeout(function(){fautoscroll()},autoscroll_interval);
		}

		jQuery('.prodscroller').hover(function() { autoscroll_lock = 1; },function() { autoscroll_lock = 0; });

		if (autoscroll) { timer = setTimeout(function(){fautoscroll()},autoscroll_interval); }
	},

	handleFakeSelect: function() {
		jQuery('.fakeselect-values3 li').click(function() {
			//jQuery('.fakeselect-values').hide();
			//jQuery('.sidebar-currency .value').html(jQuery('.text',this).html());
			//jQuery('.sidebar-currency .img').html(jQuery('.fright',this).html());
			//jQuery('.fakeselect-values li').show();
			//jQuery(this).hide();

			currency_val = jQuery('a.value',this).attr("rel");
			jQuery.ajax({
				method: "get", 
				url : "/bintime/ajax/cart/",
				data: "currency="+jQuery('a.value',this).attr("rel")+"&ajax=1",
				success: function(html) {
					jQuery('#mini_cart_wrapper').html(html);
					toute.handleFakeSelect();
					//jQuery('.fakeselect-values a.value[rel='+currency_val+']').parent().hide();
					//jQuery('.sidebar-currency .value').html(jQuery('.fakeselect-values a.value[rel='+currency_val+']').parent().contents(".text").html());
					//jQuery('.sidebar-currency .img').html(jQuery('.fakeselect-values a.value[rel='+currency_val+']').parent().contents(".fright").html());
				}
			});
		});

		// ####################################
		
		jQuery('.fake-select2').hover(
			function() {
				jQuery(this).addClass("hover");
			},
			function() {
				jQuery(this).removeClass("hover");
			}
		).click(function() {
			vals = jQuery(this).parents(".fakeselect-wrap").contents(".fakeselect2-values");
			v = vals.is(":visible");

			if (!v) {
				jQuery('.fakeselect2-values').hide();
				jQuery('.fake-select2').removeClass("opened");
			}

			pos = {
				x: Math.floor(jQuery(this).offset().left)+"px",
				y: Math.floor(jQuery(this).offset().top+jQuery(this).outerHeight())+"px",
				w: (vals.outerWidth()<jQuery(this).outerWidth()) ? Math.floor(jQuery(this).outerWidth()-2)+"px" : Math.floor(vals.outerWidth()-2)+"px"
			}

			if (v) {
				vals.scrollTop(0);
				vals.hide();
			} else {
				vals.css({"top":pos.y,"left":pos.x,"width":pos.w}).show();
			}
			jQuery(this).toggleClass("opened");
		});

		jQuery('.fakeselect2-values li').hover(
			function() {
				jQuery(this).parent().children().removeClass("on");
				jQuery(this).addClass("on");
			},
			function() {
				jQuery(this).removeClass("on");
			}
		).click(function() {
			function trim(str) {
				str = str.replace(/^\s+/, '');
				for (var i = str.length - 1; i >= 0; i--) {
					if (/\S/.test(str.charAt(i))) {
						str = str.substring(0, i + 1);
						break;
					}
				}
				return str;
			}

			jQuery('input:hidden',jQuery(this).parents(".fakeselect-wrap")).val(jQuery('a.value',this).attr("rel"));
			jQuery('.text',jQuery(this).parents(".fakeselect-wrap")).html(trim(jQuery('.li-text',this).html()));

			jQuery(this).parents(".fakeselect2-values").scrollTop(0).hide();

			jQuery('#filters-form').submit();
		});
		jQuery('.fakeselect2-values').mouseleave(function() {
			jQuery(this).scrollTop(0).hide();
			jQuery('.fake-select2',jQuery(this).parents(".fakeselect-wrap")).removeClass("opened");
		});
	},

	handleCatBlock: function() {
		jQuery('.expand-frontpage-all').click(function(e){
			e.preventDefault();

			if (!jQuery(this).hasClass("collapse")) {
				jQuery('.expand-frontpage-table .cat-expandable').show();
				jQuery('.expand-frontpage-table .icon-expand img').attr("src","/images/icon-collapse.png");
			} else {
				jQuery('.expand-frontpage-table .cat-expandable').hide();
				jQuery('.expand-frontpage-table .icon-expand img').attr("src","/images/icon-expand.png");
			}
		});

		jQuery('.expand-frontpage-table .icon-expand img').click(function() {
			jQuery('#expandable_'+jQuery(this).attr("alt")).toggle();
			jQuery(this).attr("src",jQuery('#expandable_'+jQuery(this).attr("alt")).is(":visible")?"/images/icon-collapse.png":"/images/icon-expand.png");
		});
	},

	handleCrawler: function() {
		text = jQuery('#head-crawler').html();

		text = text.replace("&amp;","&");

		newtext = "";
		for (i=0;i<text.length;i++) {
			newtext += (text.charAt(i)!=" ") ? "<span>"+text.charAt(i)+"</span>" : " ";
		}

		newtext = newtext.replace("&","&amp;");
		newtext = newtext.replace("<span>@</span>","<br/>");
		jQuery('#head-crawler').html(newtext);

		var timer = null;
		var range = 5;
		var ptr = -1;
		var elems = jQuery('#head-crawler span');
		var length = elems.length;

		changeState = function() {
			ptr++;

			if (ptr==length) { ptr = 0; }

			ids = new Array();
			for (i=0;i<range;i++) {
				ids.push(((ptr+i)>length-1) ? (ptr+i)-length : ptr+i);
			}

			elems.removeClass("hilite");
			for (i=0;i<ids.length;i++) {
				elems.eq(ids[i]).addClass("hilite");
			}

			timer = setTimeout(changeState,100);
		}

		changeState();
	},

	handleMemoryFront: function() {
		jQuery('.memoryfront td').click(function(e) {
			e.preventDefault();

			jQuery(this).parent().children().removeClass("act");
			jQuery(this).addClass("act");

			jQuery('#container-'+jQuery('a',this).attr("rel")).parent().children().hide();
			jQuery('#container-'+jQuery('a',this).attr("rel")).show();
		});
	},

	handleMisc: function() {
		jQuery('.more-views a').click(function(e) { e.preventDefault(); });
	},

	handleSidebarPaging: function() {
		jQuery('.sidebar-paging').each(function() {
			var pref = jQuery('a.pref',this).attr("rel");

			jQuery('div[id^='+pref+'-]:gt(0)').hide();

			jQuery('a.page',this).click(function(e) {
				e.preventDefault();

				jQuery('#'+pref+'-'+jQuery(this).attr("rel")).parent().children().hide();
				jQuery('#'+pref+'-'+jQuery(this).attr("rel")).show();
			});
		});
	},

	handleTabs: function() {
		jQuery('.tabs-head .wrap').hover(
			function() {
				jQuery(this).addClass("on");
			},
			function() {
				jQuery(this).removeClass("on");
			}
		);

		jQuery('.tabs-head .wrap').click(function(e) {
			e.preventDefault();

			jQuery(this).parents("table").eq(0).children().find(".wrap").removeClass("act");
			jQuery(this).addClass("act");

			jQuery('#container-'+jQuery('a',this).attr("rel")).parent().children().hide();
			jQuery('#container-'+jQuery('a',this).attr("rel")).show();
		});
	}
}

