
window.onload = function() {
	resize('bg');
	setTimeout ( function() {
		$('#productsdrop').bind("mouseleave",function() { dropout = true; productsdrop(); }); 
		$('#menuproducts').bind("mouseleave",function() { dropout = true; productsdrop(); }); 
		$("#menuproducts").mouseenter(function(){ setTimeout(function(){ dropout = false; },20);  });
		$("#productsdrop").mouseenter(function(){ setTimeout(function(){ dropout = false; },20);  });
	},1000);
};
$(window).bind("resize", function() { resize('bg'); });

function resize(id) {
	if (document.getElementById(id) == null) return false;
	var bg = document.getElementById(id); 
	//alert(bg.width+':'+($(window).width() - 350));
	if (bg.width < ($(window).width() - 350) || bg.height > $(window).height()) {		
		bg.style.height = 'auto';
		bg.style.width = ($(window).width() - 350) + 'px';
		
	}
	if (bg.height < $(window).height()) {
		bg.style.width = 'auto';
		bg.style.height = '100%';
	}	
}

function validate_contact(type) {
	if (type == 'chas') var fields = new Array('name','phone','mess');
	else var fields = new Array('name','phone','email','subject','mess');
	
	for (var field in fields) {
		if (document.forms["contact"][fields[field]].value == '') {
			alert("Моля, попълнете всички полета!");
			return false;
		}
	}
	
	if (type != 'chas') {
		var email = document.forms["contact"]["email"].value;
		var atpos = email.indexOf("@");
		var dotpos = email.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2 >= email.length) {
			alert("Електронната Ви поща Не е валидна!");
			return false;
		}	
	}
}

function show(id) {
	document.getElementById(id).style.display = 'block';
}
var nn6 = document.getElementById&&!document.all;

dropout = true;
dropblock = false;
function productsdrop() {
	if (document.getElementById('dropanim').style.marginTop != '0px') {
		dropblock = true;
		$('#dropanim').animate({ marginTop:   '0px' }, 300, function() { dropblock = false; } );
		document.getElementById('menuproducts').className = 'hover';
	}
	setTimeout( function() {
		if (dropout == true && dropblock == false) {
			$('#dropanim').animate({ marginTop: '-650px' }, 300);
			document.getElementById('menuproducts').className = '';  
		}
	}, 50);
}


function drop_expand(obj,rows) {
	dropblock = true;
	dropout = false;
	var height = (Math.ceil(rows)*125) + 60 + 'px';
	var parent = obj.parentNode;
	var up = parent.id + '-up';
	if (parent.style.height != height) {
		$(parent).animate({ height: height }, 500, function() { document.getElementById(up).style.visibility = 'visible'; 
			setTimeout(function(){ dropblock = false; }, 200); } );
		if (parent.id != 'dropkozmetika' ) 
			$('#dropkozmetika' ).animate({ height: '40px' }, 500, function() { document.getElementById('dropkozmetika-up').style.visibility = 'hidden'; } );
		if (parent.id != 'dropoborudvane') 
			$('#dropoborudvane').animate({ height: '40px' }, 500, function() { document.getElementById('dropoborudvane-up').style.visibility = 'hidden'; } );
		if (parent.id != 'dropaparati'   ) 
			$('#dropaparati'   ).animate({ height: '40px' }, 500, function() { document.getElementById('dropaparati-up').style.visibility = 'hidden'; } );
	}
	else $(parent).animate({ height: '40px' }, 500, function() { document.getElementById(up).style.visibility = 'hidden'; 
			setTimeout(function(){ dropblock = false; }, 200); } );
}

last_info = 'info';
function prod_info(id) {
	document.getElementById(id).style.display = 'block';
	document.getElementById(id + '_button').className = 'hover';
	if (id != last_info) {
		document.getElementById(last_info).style.display = 'none';
		document.getElementById(last_info + '_button').className = '';
	}
	last_info = id;
	
}
function gallery(open,obj) {
	if (open != 'close' || document.getElementById('center').style.marginLeft != '-500px') {
		if (document.getElementById('dropanim').style.marginTop == '0px') {	
			dropout = true;
			dropblock = false;
			productsdrop();
		}
		
		$( "#center").animate({ marginLeft: '-500px' }, 1000 );
		$( "#centerbg").animate({ left: '-250px' }, 1000 );
		$( ".thumbs").animate({ left: '380px' }, 1000 );
		$(".gallery").animate({ marginLeft:  '350px' }, 1000 , function() {      
			document.getElementById('gallery-arrow-open').style.visibility = 'hidden';
			document.getElementById('gallery-arrow-close').style.visibility = 'visible';
			if (open != 'open') setTimeout(function() { gallerybg(open,obj); }, 100);
		});
	}
	else {
		$( "#center").animate({ marginLeft:   '0px'}, 1000 );
		$( "#centerbg").animate({left: '250px' }, 1000 );
		$( ".thumbs").animate({ left: '850px' }, 1000 );
		$(".gallery").animate({ marginLeft: '850px'}, 1000 , function() {      
			document.getElementById('gallery-arrow-close').style.visibility = 'hidden';
			document.getElementById('gallery-arrow-open').style.visibility = 'visible';
		});
	}
}

function gallerybg(file,obj) {	
 	if (document.getElementById('center').style.marginLeft != '-500px') {
		gallery(file,obj);		
	}		
	else if (file.match(/jpg|png/i)) {	
		if (document.getElementById('bg').src == file) return false;
		document.getElementById('media').style.display = 'none';
		
		resize('bgtemp');
		document.getElementById('bgtemp').style.visibility = 'visible';
		
		$('#bg').attr('src', file);
		setTimeout(function() { 
			resize('bg'); 		
			$('#bgtemp').fadeOut(600, function() {
				document.getElementById('bgtemp').style.visibility = 'hidden';
				document.getElementById('bgtemp').src = file;
				$('#bgtemp').fadeIn('fast');
			});	 
		},100);
	} 
	else {
		document.getElementById('media').style.display = 'block';
		document.getElementById('mediawindow').innerHTML = '<div id="mediaplayer" class="mediaplayer"></div>';
		jwplayer('mediaplayer').setup({
			'flashplayer': 'player.swf',
			'id': 'playerID',
			'width': '480',
			'height': '330',
			'file': file
		});
	}
	$('.curthumb').removeClass();
	obj.parentNode.className = 'curthumb';
}

/* ---------------------------------------------------------------------------
 * Close all opened Popups
 *
 * @param {String}  e  Event
 * ---------------------------------------------------------------------------
 */
function close_popups(e) {
	var fobj = nn6 ? e.target : event.srcElement;
	var obj = fobj;
	
	var popups = new Array('aparati','oborudvane');
	var exclude = 'menuproducts';
	if ((!fobj.id || exclude.indexOf(fobj.id) == -1)) {
		for (var popup in popups) {
			
			if (document.getElementById(popups[popup]) == null || fobj.id == popups[popup] + '-open') continue;
			
			if (parent_exists(obj,popups[popup]) === false) {
				document.getElementById(popups[popup]).style.display = 'none';
			}

		}
	}
}
/* ---------------------------------------------------------------------------
 * Check if parent exists
 *
 * @param {Number} obj	  object
 * @param {Number} parent Id of parent
 * ---------------------------------------------------------------------------
 */
function parent_exists(obj,parent) {	
	if (document.getElementById(parent) == null) return 0;
	
	for (var i=1; i<15; i++) {
		if (obj == null) break;
		if (obj != null && obj.id == parent) {
			return true;
			break;
		}				
		obj = obj.parentNode;
	}
	return false;
}


function mdown(e) {
	close_popups(e);
}
document.onmousedown = mdown;
