function expand(eid,pm) {
	e=document.getElementById(eid).style
	e.display=(e.display=='block')?'none':'block';
	if (pm) { pm.style.backgroundImage=(e.display=='block')?'url(/img/expand-minus.gif)':'url(/img/expand-plus.gif)' }
}
function basketremove(e) { if (e.options[e.selectedIndex].value=="remove") e.parentNode.getElementsByTagName('input')[0].value=0; }

function dropdown(drop_down_list) {
	var number = drop_down_list.selectedIndex;
	location.href = drop_down_list.options[number].value;
}

/**
 * @author Leha Stratan | summerrain19@gmail.com
 *
 * 05.02.2010
 */


$(document).ready(function(){
	PlaceHolder("input.placeholder");
	Gallery();
	Search();

	$('dl.tabs dt').click(function(){
		$(this)
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
	});


});

function Search(){
	var f = $("#search-form");
	f.find("select").change(function(){
		console.log("sdfsd");
    	var n = $(this).find("option:selected").val();
    	if (n == 0) {
    		f.attr({action: "/catalogue/"});
    	} else {
    		f.attr({action: "/catalogue2/"})
    	}
    });
}

function PlaceHolder(selector){
	$(selector).each(function(){
		var defaultText = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == defaultText ){
				$(this).val("");
			}
		});
		 $(this).blur(function(){
			if ($(this).val() == ""){
				$(this).val(defaultText);
			}
		});
	})
}

function Gallery(){
	var current = 0;

	$(".good-photos a").each(function(index){
		$(this).click(function(){
			popup.open();
			bigimg.load($(this).attr("href"), index);
			current = index;
			return false;
		});
	});

	var popup = $(".popup");
	var popupWidth = 420;

	popup.open = function(){
		popup.place(popupWidth);
		popup.show();

		$(document).bind("click", function(e){
			if (here == false) {
				popup.close();
			}
		});

		$(document).bind("keydown", function(event){
			var B = event.keyCode;
			switch (B) {
			    case 39:
			    	if (event.ctrlKey) {
			    		next();
					}
					break;
		    	case 37:
		    		if (event.ctrlKey) {
			    		prev();
		    		}
			    	break;
			    case 27:
					popup.close()
			}
		});
	}
	popup.place = function(popupWidth){
		var yScroll;

		//на сколько документ проскроллен
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}


		if (yScroll > 0 ) {
			yScroll += 10;
			popup.css({top: yScroll + "px"});
		}

		popup
			.width(popupWidth + "px")
			.css({marginLeft: - popupWidth / 2 + "px"});
	}
	popup.close = function(){
		popup.hide();
		$(document).unbind("click");
		$(document).unbind("keydown");
		window.clearInterval(checkInterval);
	}


	var here = true;
	popup.mouseout(function(){
		here = false;
	});
	popup.mouseover(function(){
		here = true;
	});




	popup.find(".popup-close").click(function(){
		popup.close();
	})

	var thumbs = popup.find(".popup-thumb");
	thumbs.each(function(index){
		var a = $(this).find("a");
		a.click(function(){
			bigimg.load($(this).attr("href"), index);
			return false;
		})
	});

	popup.find(".popup-prev").click(function(){
		prev();
		return false;
	});

	popup.find(".popup-next").click(function(){
		next();
		return false;
	});

	var prev = function(){
		bigimg.load("", current - 1);
		return false;
	}

	var next = function(){
		bigimg.load("", current + 1);
		return false;
	}

	var bigimg = popup.find(".popup-big-img");

	bigimg.click(function(){
		next();
	});

	var checkInterval;

	bigimg.load = function(href, index){
		if (href == "") {
			if (index == -1) {
				index = thumbs.length - 1;
			}
			else if (index == thumbs.length) {
				index = 0;
			}
			href = thumbs.eq(index).find("a").attr("href");
		}

		current = index;

//		bigimg.attr({src: "/img/ajax-loader.gif"});
		bigimg.attr({src: href});

		checkInterval = window.setInterval(function(){
//			console.log(bigimg.width());
			if (bigimg.width() > popupWidth - 20) {
				popup.place(bigimg.width() + 20);
				window.clearInterval(checkInterval);
			}
		}, 500)

		thumbs.removeClass("current");
		thumbs.eq(index).addClass("current");
	}

}
