/**
* @author: Raducanu Marius
* @email: marius.raducanu@mapixels.com
* Special pentru JocuriMini.ro
**/
// Constants 
	setInterval("jmNext()", 6000);
var the_form = '<form method="post" action="" id="form_feedback"><input type="hidden" name="ip_address" id="ip_address" value="' + ip_address +'" /><label for="feedback_msg">Parerea despre site</label><textarea name="feedback_msg" id="feedback_msg" cols="67" rows="8"></textarea><label for="verificare">Intoruceti exact textul din imagine(litere mari)</label> <span style="position: relative"><img src="includes/verification_image.php" id="contact_code" width="100" height="30" border="0" alt="" /> <br/><input type="text" name="verificare" id="verificare" style="width: 100px; position: absolute; top: -20px; #top: 0px; _top: 0px; left: 120px;" /></span><label for="submit_feed"></label><input type="submit" id="submit_feed" value="Trimite" /><em id="feed_load">Se trimite...</em></form>';
// DOM ready
$(function(){
	// search submit form
	$('input#search_submit').click(function(){
		var searchWord = $('#search_word').val();
			searchWord = trim(searchWord);
			if(searchWord.length < 3) {
				alert('Introdu cel putin 3 caractere pentru cautare \n');
				return false;
			}else{
				return true;
			}
	});
	// top menu / submenu change
	$('#top_menu li a').mouseover(function(){ 
		var subMenu = $(this).attr('name');
		if(subMenu == '' || subMenu == 'subTop'){
			return false;
		}else{
			$('.sub_menu').hide();
			$('#' + subMenu).show();
		} 
	});
	//Show Banner
	$(".main_image .desc").show(); 
	$(".main_image .block").animate({ opacity: 0.65 }, 1 ); 
 
	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); 
		var imgTitle = $(this).find('a').attr("href"); 
		var imgDesc = $(this).find('.block').html(); 	
		var imgDescHeight = $(".main_image").find('.block').height();
		var newHref = $(this).find('a').attr('title');
		
		if ($(this).is(".active")) {  
			return false; // 
		} else {
			
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.65,	marginBottom: "0" }, 250 );
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 250 );
				$(".main_image .big_link").attr('href', newHref);
			});
		}
		
		$(".image_thumb ul li").removeClass('active');
		$(this).addClass('active'); 
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
	
	 // scroll to top
		$('#totop').click(function(){
			$('html, body').animate({scrollTop:0}, 'slow');
		});
	
	// feedback
	$('#feed_drop').unbind().click(function(){
			if($('#feedback').height() < 5) {
				$(this).removeClass('feedback_btn').addClass('active_btn');
				$('#feedback').prepend(the_form).addClass('active_fb').css('z-index', '999').hide().fadeIn();

			}else{
				$(this).removeClass('active_btn').addClass('feedback_btn');
				$('#feedback').removeClass('active_fb');
				$('#form_feedback').remove();
			}
				
	});
	
	// submit feed 
	$('#submit_feed').live('click', function(){
		$('#error_msg').remove();
		$(this).attr('disabled', 'disabled');
		$("#feed_load").fadeIn("fast");
		var captcha = $('#verificare').val();
			captcha = trim(captcha);
		var mesaj = $('#feedback_msg').val();
			mesaj = trim(mesaj);
		var ip = $('#ip_address').val();
		var er = false;
		var er_msg = '';
		if(mesaj.length < 10)	{
			er = true;
			er_msg += 'Mesajul dvs trebuie sa contina cel putin 10 caractere <br/>'
		}
		if(ip.length < 10)	{
			er = true;
		}
		if(captcha.length != 4) {
			er = true;
			er_msg += 'Codul din imagina are 4 caractere, dvs ati introdus '+ captcha.length + ' <br/>';
		}
		if(er == true){
			$('#feed_load').animate({opacity: 1}, 300, function(){
				$('#feed_load').hide();
				$('<div id="error_msg">' + er_msg +'</div>').insertAfter(this);
			});
			$(this).attr('disabled', false);
		}else{
			$.post(
				'ajax.php',
				{action: 'add_feedback', mesaj: mesaj, captcha: captcha, ip: ip, pagina: curr_page},
				function(result){
					$('#feed_load').animate({opacity: 1}, 300, function(){
						$('#feed_load').hide();
						$('#submit_feed').attr('disabled', false);
					});
					
					if(result.ok == false){
						$('<div id="error_msg">' + result.msg +'</div>').insertAfter('#submit_feed');
					}else{
						$('#feedback_msg').val('');
						$('#verificare').val('');
						$('<div id="success_msg">' + result.msg +'</div>').insertAfter('#submit_feed');
						setTimeout(function(){$('#feed_drop').trigger('click');}, 3000);
					}
				},
				'json'
				
			);
		}
		return false; 
	});
});
	
// Other Functions
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,'');
}
function jmNext()
{
	var curr = $('li.active');
	var next = (curr.next().length == 0 ?  $(".image_thumb ul li:first") : curr.next());
	next.trigger('click');
}
