// JavaScript Document
//nivo slider
$(window).load(function() {
	$('#slider').nivoSlider({
		directionNav: false,
		pauseTime: 3500,
		controlNav: false	
	});
});

$(document).ready(function(){
	//shadowbox
    var options = {
        handleOversize	: "drag",
		modal			: true,
		continuous		: true,		
        //displayNav:         false,
    };
    Shadowbox.init(options);	
	//menu	
	var ses 	= $.namesession.get('menu');
	var sessub 	= $.namesession.get('submenu');
	//ul_result
	$('#menu_left ul').hide();
	$('#ul_result ul').hide();
	
	$('ul#menu_left li a.parent').each(function() {
		$(this).click(function() {
			var text	= $(this).text();
			if(!$(this).hasClass('active')) {
				$(this).addClass('active');
				$(this).parent().children('ul').slideDown('normal');
			} else {
				$(this).removeClass('active');
				$(this).parent().children('ul').slideUp('normal');
			}
			$.namesession.set('menu', text);
			//return false;
		});
		
		if($(this).hasClass('active')) {
			$(this).parent().children('ul').css('display', 'block');
		}
		if($(this).text() == ses) {
			//alert($(this).parent().children('ul').attr('id'));
			$(this).parent().children('ul').show();
			$(this).addClass('active');
			$(this).parent().children().children().each(function() {
				//alert($(this).children('a').text());
				if($(this).children('a').text() == sessub) {
					$(this).children('a').addClass('active');
				}
			});
		}
	});

	$('ul#menu_left li ul li a').click(function() {
			var text = $(this).text();
			$.namesession.set('submenu', text);
	});	

	$('ul#ul_result li a.parent').each(function() {
		$(this).click(function() {
			var text	= $(this).text();
			if(!$(this).hasClass('active')) {
				$(this).addClass('active');
				$(this).parent().children('ul').slideDown('normal');
			} else {
				$(this).removeClass('active');
				$(this).parent().children('ul').slideUp('normal');
			}
			$.namesession.set('menu', text);
			//return false;
		});
		
		if($(this).hasClass('active')) {
			$(this).parent().children('ul').css('display', 'block');
		}
		if($(this).text() == ses) {
			//alert($(this).parent().children('ul').attr('id'));
			$(this).parent().children('ul').show();
			$(this).addClass('active');
			$(this).parent().children().children().each(function() {
				//alert($(this).children('a').text());
				if($(this).children('a').text() == sessub) {
					$(this).children('a').addClass('active');
				}
			});
		}
	});

	$('ul#ul_result li ul li a').click(function() {
			var text = $(this).text();
			$.namesession.set('submenu', text);
	});	


	
	$('#vertSlider').cycle({ 
		fx:     'scrollHorz', 
		speed:  '800', 
		timeout: 0, 
		next:   '.next', 
		prev:   '.prev' 
	});		
	
	//captchaRefresh
	$('#divcaptcha').captchaRefresh({src: base_url+'/inc/xfile/captcha.php'});
	//contact form
	$('#f_contact').validate({
		rules: {
			name			: {
					required	: true,
					maxlength	: 50
			},
			email			: {
					required	: true,
					email		: true
			},
			subject			: 'required',
			phone			: 'required',
			message			: 'required',
			security_code	: 'required'
		},		
		submitHandler: function() {
			$.post($('#f_contact').attr('action'),{ 
					name			: $('#name').val(),
					email			: $('#email').val(),
					company			: $('#company').val(),
					subject			: $('#subject').val(),
					phone			: $('#phone').val(),
					message			: $('#message').val(),
					security_code	: $('#security_code').val(),
					submit_contact	: true
				}, function(data){
					if(data == 1) {
						jAlert("Contact Form Submited","Success");
						$("#popup_ok").click(function(){
							window.location = base_url;
						});								
					} else if(data == 2){
						jAlert("Sorry, the security code that you entered is incorrect.<br/>Please try to submit this form again with the correct code.","Error");
						$("#popup_ok").click(function(){
							$('#reload').click();
						});	
					
					} else {
						jAlert("Submit Failed. Please Try Again.","Error");	
						//jAlert(data,"Error");	
						$("#popup_ok").click(function(){
							$('#reload').click();
						});							
					}
				}
			);
			return false;
		}	
	});
	$('#f_contact_report').validate({
		rules: {
			name			: {
					required	: true,
					maxlength	: 50
			},
			email			: {
					required	: true,
					email		: true
			},
			phone			: 'required',
			message			: 'required',
			security_code	: 'required'
		},		
		submitHandler: function() {
			$.post($('#f_contact_report').attr('action'),{ 
					name			: $('#name').val(),
					email			: $('#email').val(),
					company			: $('#company').val(),
					subject			: $('#subject').val(),
					phone			: $('#phone').val(),
					message			: $('#message').val(),
					security_code	: $('#security_code').val(),
					submit_contact_report	: true
				}, function(data){
					if(data == 1) {
						jAlert("Contact Form Submited","Success");
						$("#popup_ok").click(function(){
							window.location = base_url;
						});								
					} else if(data == 2){
						jAlert("Sorry, the security code that you entered is incorrect.<br/>Please try to submit this form again with the correct code.","Error");
						$("#popup_ok").click(function(){
							$('#reload').click();
						});	
					
					} else {
						jAlert("Submit Failed. Please Try Again.","Error");	
						//jAlert(data,"Error");	
						$("#popup_ok").click(function(){
							$('#reload').click();
						});							
					}
				}
			);
			return false;
		}	
	});
	//gallery photo 
	$("#pikame").PikaChoose({carousel:true, carouselVertical:true});
	//wmode youtube
	$('iframe').each(function(){
		var url = $(this).attr("src");
		$(this).attr("src",url+"?wmode=transparent");
	});	
	//running text
	$('.horizontal_scroller').SetScroller({	
		velocity		: 60,
		direction		: 'horizontal',
		startfrom		: 'right',
		loop			: 'infinite',
		movetype		: 'linear',
		onmouseover		: 'pause',
		onmouseout		: 'play',
		onstartup		: 'play',
		cursor			: 'pointer'
	});	
	$('#nav li').hover(function (){
		$('ul', this).slideDown(30);
		},function () {
			//hide its submenu				
			$('ul', this).slideUp(100);						
		}
	);		
	
});
