$(document).ready(function() {
	
	/*if ($.browser.chrome) {
		alert($.browser.version);
	}*/
	
	//loginWin();
	var menuID=0;
	$(".top_nav_menu > a").each(function(){
		menuID++;
		$(this).attr('id', 'topmenu_'+menuID);
	});
	
	$(".top_nav_menu").hover(function(){
		if($(this).find('div[class*=top_nav_dropdown]').length != 0)
			$(this).addClass("menu_hover");
	},function(){
		if($(this).find('div[class*=top_nav_dropdown]').length != 0)
			$(this).removeClass("menu_hover");
	});

	$('.search_input').restoreInput();
	$('.newsletter input').restoreInput();
	
	setUpLeftMenus();
	
	$('.submit').formSubmit();
	
	// dropdown kategoria linkjeinek tordelese ha hoszabb mint 4 szo
	$('.dropdown_categ > a').each(function() {
		var worlds = $(this).text().split(' ');
		if(worlds.length > 4) {
			for (var i in worlds) {
				if(i == 3 || i == 7 || i == 11)
					worlds[i] = worlds[i]+'<br />';
			}
			$(this).html(worlds.join(' '));
		}
	});

	//VAKBARAT
	if($('link[href*=blind]').length != 0) {
      	$('.menu1 .dropdown_categ a').appendTo('.menu1 .top_nav_dropdown1');
		$('<br />').insertAfter('.menu1 .top_nav_dropdown1 a');
		$('.menu1 .top_nav_dropdown1').css({padding: 10});
		$('.top_nav_menu > a').css({color: 'red'});
		$('.menu1 .top_nav_dropdown1 table').remove();
		$('.top_nav').prependTo('.content_left');
		$('.featured_item_title, .leftmenu_maincat').attr('style', '');
		$('.leftmenu').prependTo('.content_left');
	}


});

$(window).load(function() {
	$('.container2').centerAbsolute();

	if($('.slide_scroll').length != 0)
		imageSlideSetUp();
});



var imageSlideScroll = 0;
function imageSlideSetUp() {
	var oneitem = $('.slide_item').get(0);
	slide_item_width = $(oneitem).outerWidth(true);	
	$('.slide_scroll').width($('.slide_scroll div').length*slide_item_width);
	if($('.slide_slide').width()+10 <= $('.slide_scroll').width())
		$('.slide_right').addClass('slide_act');
	$('.slide_right').click(function(e) {
		if(($('.slide_scroll div').length*slide_item_width)-$('.slide_slide').width() > imageSlideScroll){
			if(($('.slide_scroll div').length*slide_item_width)-$('.slide_slide').width()-imageSlideScroll >= slide_item_width) {
				$('.slide_slide').stop().animate({scrollLeft:  imageSlideScroll+slide_item_width}, 300);
				imageSlideScroll = imageSlideScroll+slide_item_width;
				$('.slide_left').addClass('slide_act');
				if((($('.slide_scroll div').length*slide_item_width)-$('.slide_slide').width())-slide_item_width < imageSlideScroll)
					$('.slide_right').removeClass('slide_act');
			}
		}
	});
	
	$('.slide_left').click(function(e) {
		if(imageSlideScroll != 0) {
			$('.slide_right').addClass('slide_act');
			$('.slide_slide').stop().animate({scrollLeft:  imageSlideScroll-slide_item_width}, 300);
			imageSlideScroll = imageSlideScroll-slide_item_width;
			if(imageSlideScroll == 0) {
				$('.slide_left').removeClass('slide_act');
			}
		}
		
	});
}




$.fn.restoreInput = function() {
	$(this).each(function() {
		$(this).attr('fvalue', $(this).val());
		$(this).focus(function() {
			if($(this).val() == $(this).attr('fvalue') || $(this).val() == "")
				$(this).val('');
		}).blur(function() {
			if($(this).val() == $(this).attr('fvalue') || $(this).val() == "")
				$(this).val($(this).attr('fvalue'));
		});
	});
};

$.fn.centerVertical = function() {
	$(this).each(function() {
		$(this).css({marginTop: '-'+$(this).outerHeight()/2+'px', position: 'absolute', top: '50%'});
	});
};


$.fn.centerAbsolute = function() {
	$(this).each(function() {
		if($(this).outerHeight() < $(this).parent().innerHeight())
			$(this).css({marginTop: '-'+$(this).outerHeight()/2+'px', position: 'absolute', top: '50%', marginLeft: '-'+$(this).outerWidth()/2+'px', left: '50%'});
		else
			$(this).css({marginTop: 0, top: 0});
			
		if($(this).outerWidth() < $(this).parent().innerWidth())
			$(this).css({position: 'absolute', marginLeft: '-'+$(this).outerWidth()/2+'px', left: '50%'});
		else
			$(this).css({marginLeft: 0, left: 0});
			
		if(typeof( centerabs ) == "undefined") {
			centerabs = 1;
			$(window).resize(function() {
				$('[centerabs=centerabs]').centerAbsolute();
			});
		}
		$(this).attr("centerabs", "centerabs");
	});
};


$.fn.formSubmit = function() {
	$(this).each(function() {
		$('<input type="submit" name="submit" value="submit" />').css('display', 'none').insertAfter($(this));
		$(this).click(function() {
			$(this).next().click();
			return false;
		});
	});
	
};

function loginWin() {
	$('#login_win').css('left', 400).css('top', $('#login_lnk').offset().top+20);
	$('#login_lnk').click(function() {
		$('#login_win').toggle();
	});
}

function setUpLeftMenus() {
	$('ul[class=nav] > li').each(function(e) {
		//$(this).find('ul').slideUp(0);
		if(!$(this).hasClass('leftmenu_open'))
			$(this).find('ul').slideUp(0);
			
		$(this).click(function(e) {
			if($(this).find('ul').css("display") == "none") {
				$(this).parent().find('li > ul').slideUp(300);
				$(this).find('ul').slideDown(300);
			}
			
		});
	});
}

function shippingAdressCopy(o) {
	if(o.checked == true) {
		$('#shipping_name').val($('#billing_name').val());
		$('#shipping_city').val($('#billing_city').val());
		$('#shipping_zip').val($('#billing_zip').val());
		$('#shipping_street').val($('#billing_street').val());
	}
	if(o.checked == false) {
		$('#shipping_name').val('');
		$('#shipping_city').val('');
		$('#shipping_zip').val('');
		$('#shipping_street').val('');
	}
}

function goCompare() {
	compareError = 0;
	var selectedcatid = 0;
	var categid = 0;
	var categs = $('input[name*=to_compare]').each(function() {
		if($(this).attr('checked') == true) {
			var categid = $(this).attr('category_id');
			//console.log(categid);
			if(selectedcatid != 0) {
				if(categid != selectedcatid) {
					//console.log("nem egyforma");
					compareError = 1;
				}
			}
			else
				selectedcatid = categid;
		}
	});
	if(compareError == 0) {
		$('#compare_items').submit();
	}
	else {
		popupWindow('Csak egyazon kategórián belüli termékeket tud összehasonlítani!');
		return false;
	}
}
