// Tabs on the main page
/*function opentab(tab) {
	var tabtags = document.getElementById('tabtags');
	var newstab = document.getElementById('newstab');
	var photoestab = document.getElementById('photoestab');
	
	if (tab == 'photoestab') {
			photoestab.style.display = 'block';
			newstab.style.display = 'none';
	} else {
		photoestab.style.display = 'none';
		newstab.style.display = 'block';
	}
	
	tabtags.className = tab;
	
	return false;
}*/

// Dropdown menu
/*startList = function() {
		if (document.all&&document.getElementById) {
				var navRoot = document.getElementById("navRoot");
				for (i=0; i<navRoot.childNodes.length; i++) {
						var node = navRoot.childNodes[i];
						if (node.nodeName=="LI") {
								node.onmouseover=function() {
										this.className+=" over";
										
										//console.log(this.className);
								
								}
								node.onmouseout=function() {
										this.className=this.className.replace("over", "");
										
										//console.log(this.className);
								}
						}
				}
		}
}*/
//window.onload=startList;

// @obg jQuery selector The ID of the fancy dropdown
function fancySelect(obj) {
	var list = $(obj+".fancy-select ul");
	var options = $(obj+".fancy-select ul a");
	$(obj+".fancy-select").hover(
		function(){
			list.slideDown(80);
		},
		function(){
			list.slideUp(80);
		}	);
	
	// Setting value
	options.click(function(){
		$(obj+".fancy-select .title").html($(this).html());
		options.removeClass('active');
		$(this).addClass('active');
		//return false;
	});
}

//Adjust width of a search field on focs and revert to original width on blur
function adjustWidthOfASearchInput() {
	var input = $("#filter .search. .textfield");
	var original_width = input.css('width');
	input.focus(function(){
			input.stop().animate({'width': 312}, 150);
			if (input.attr('value') == 'Поиск...') { input.attr('value', ''); }
	});
	
	input.blur(function(){
			//input.css('width', 112);
			input.stop().animate({'width': original_width}, 130);
			if (input.attr('value') == '') { input.attr('value', 'Поиск...'); }
	});
}

function displayValueOfCustomFileInput() {
	//if (!$.browser.msie && jQuery.browser.version != '6')
	var finput = $(".custom-file-input .file");
	//The first parent is the label, only the second is the container we need
	finput.parent().parent().prepend('<em class="filename"></em>');
	var fname = finput.parent().parent().find('.filename');
	
	finput.change(function(){
		fname.html(finput.attr('value'));
	});
}

// Search fields in the header and footer
// Should submit on click on form and should not on click in textfield
function stopIt(e) {
	try {
		window.event.cancelBubble = true;
	}
	catch (err) {
		e.stopPropagation();
	}
}

// Projects slider
function projectSlider(p, b, i) {
	var project = p;//'#best-buy';
	var browser = b;//'.product';
	var item = i;//'li';
	
	if (!$(project+' '+browser)) { return; }
	
	$(project+' '+browser+'>'+item+':gt(0)').hide();
	$(project+' '+browser+'>'+item+':first').addClass('first');
	$(project+' '+browser+'>'+item+':last').addClass('last');
	var cur = $(project+' '+browser+'>'+item+':first'); // Current slide
	//var curInd = $(project+' .slides-nav li:first'); // Current slider indicator
	
	// Set counter values
	var counter = $(project+' .count');
	var total = $(project+' '+browser+'>'+item).length;
	var index = 1;
	counter.html(index+'/'+total);
	//console.log(cur);
	
	// show next project
	$(project+' a.rarr').click(function(e){
			if (!cur.hasClass('last')) {
				cur.fadeOut(300);
				setTimeout(function() {
					cur = cur.next();
					cur.fadeIn(300);
					/*curInd.removeClass('active');
					curInd = curInd.next()
					curInd.addClass('active');*/
					index++;
					counter.html(index+'/'+total);
				}, 300);
			}
			
			return false;
	});
	
	// show previous project
	$(project+' a.larr').click(function(e){
		if (!cur.hasClass('first')) {
			cur.fadeOut(300);
			setTimeout(function() {
				cur = cur.prev();
				cur.fadeIn(300);
				/*curInd.removeClass('active');
				curInd = curInd.prev()
				curInd.addClass('active');*/
				index--;
				counter.html(index+'/'+total);
			}, 300);
		}
			
		return false;
	});
	
	/*$(project+' .slides-nav li a').click(function(e){
		var a = $(this);
		var slideIndex = a.attr('href').substr(1);
		
		// Activate curent slide indicator
		curInd.removeClass('active');
		curInd = a.parent();
		curInd.addClass('active');
		
		// Show slide of given index
		cur.fadeOut(300);
		setTimeout(function() {
			cur = $('#project .browser li:eq('+slideIndex+')')
			cur.fadeIn(300);
		}, 300);
		
		return false;
	});*/
}

jQuery(function($){
		// Navigation drop shadow text
		//$('#nav a, #subnav a, #head .phone').each(function(){$(this).textDropShadow();});
		
		// Zebra table
		$('table.data tr:even').addClass('even');
		// Антон, в профиле заказов зебра глючит
		$('table.data.pending-orders tr').removeClass('even');
		$('table.data.delivered-orders tr').removeClass('even');
		
		//Fancy dropdown list of manufacturers
		fancySelect('#manufacturers-select');
		fancySelect('#qty-select');
		
		
		
		//Adjust width of a search field
		adjustWidthOfASearchInput();
		
		displayValueOfCustomFileInput();
		
		// Best buy
		projectSlider('#best-buy', '.product', 'li');
		// Gadgets
		projectSlider('#gadgets', '.gadgets-body', 'ul');
});

