/* function area */
function v(obj){
	if (window.console && window.console.log)
			window.console.log(obj);	
}
function intval (mixed_var, base) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Rafa? Kukawski (http://kukawski.pl)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
    var tmp;

    var type = typeof(mixed_var);

    if (type === 'boolean') {
        return +mixed_var;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var)) {
        return mixed_var | 0;
    } else {
        return 0;
    }
}

$('#aterms').click(function(){
	$('#terms').fadeIn();
	$('#bmask')
	.css({
		height : $(document).height()
	})
	.show();
	$('#privacy').fadeOut();
	return !1;
})

$('#aprivacy').click(function(){
	$('#privacy').fadeIn();
	$('#bmask')
	.css({
		height : $(document).height()
	})
	.show();
	$('#terms').fadeOut();
	return !1;
})

$('.tclose').click(function(){
	$('#privacy').fadeOut();
	$('#terms').fadeOut();
	$('#bmask').hide();
	return !1;
})

$('#email')
.focus(function(){
	$(this)
	.css({
		'background' : 'none'
		,'backgroundColor' : '#191919'
	})
})
.blur(function(){
	if(empty($(this).val())){
		$(this)
		.css('background','#191919 url(images/email_bg.png) no-repeat 0 0')
	} else {
		$(this)
		.css({
			'background' : 'none'
			,'backgroundColor' : '#191919'
		})	
	}
})
.keypress(function(event){
	if ( event.which == 13 ) {
		loginCheck();
	}
})


$('#pwd')
.focus(function(){
	$(this)
	.css({
		'background' : 'none'
		,'backgroundColor' : '#191919'
	})
})
.blur(function(){
	if(empty($(this).val())){
		$(this)
		.css('background','#191919 url(images/password_bg.png) no-repeat 0 0')
	} else {
		$(this)
		.css({
			'background' : 'none'
			,'backgroundColor' : '#191919'
		})	
	}
})
.keypress(function(event){
	if ( event.which == 13 ) {
		loginCheck();
	}
})

$('a.btn_login').click(function(){
	$('.not_login span').css('opacity',0);

	loginCheck();
	return !1;
})

var loginCheck = function(){
	$.ajax({
	   type: "POST",
	   url: "_login.php",
	   data: ({
			'email' : $('#email').val()
			,'passwd' : $('#pwd').val()
	   }),
	   success: function(msg){
	   if(msg == '2'){
			$('.yes_login').fadeOut('500',function(){
				$('.not_login').fadeIn('500');
			});
						
			$('.not_login span')
			.stop(1,1)
			.animate({
				'opacity' : 1
			}, 500)
			
			$('#header-menu').stop(1,1).animate({
				width : 547
			}, 1000, 'easeOutElastic')
		} else {
			$('.not_login').fadeOut('500',function(){
				$('.dealer_name').html(msg);
				$('.yes_login').fadeIn('500');
			});

			
			$('#header-menu').animate({
				width : 650
			}, 1000, 'easeOutElastic')
		}
	   }
	 });
}

$('a.btn_logout').click(function(){
	$('#pwd').val('');

	$('.not_login span')
	.css({
		'opacity' : 0
	})
	
	$.ajax({
	   type: "POST",
	   url: "_login.php",
	   success: function(msg){
			$('.yes_login').fadeOut('500',function(){
				$('.not_login').fadeIn('500');
			});
			$('#header-menu').stop(1,1).animate({
				width : 547
			}, 1000, 'easeOutElastic')
	   }
	 });
	return !1;
})

//loaded DOM
$(document).ready(function(){
	resizeBg();

	//menu a
	var menu_status = 2;
	
	$('#header-menu a.ns')
	.mouseover(function(){
		switch(menu_status){
			case 1:
				var pos;
				
				pos = $(this).css('backgroundPosition');
				
				if (pos == 'undefined' || pos == null) {
					pos = $(this).css("background-position-x"); //die in hell
				} else {
					pos = pos.split(" ")[0];
				}
				
				
				$(this).animate({
					'backgroundPosition' : pos+' 0'
				},500)	
				break;
				
			case 2:
				$(this)
				
				.animate({
					opacity: 0
				},100,function(){
					$(this)
					.css("background-position-y", 0)
					.animate({
						opacity: 1
					},100)
				})
				
				break;
		}
		
	})
	.mouseout(function(){
		switch(menu_status){
			case 1:
				var pos, pos_arr;
				
				pos = $(this).css('backgroundPosition');
				
				if (pos == 'undefined' || pos == null) {
					pos = $(this).css("background-position-x"); //die in hell
				} else {
					pos = pos.split(" ")[0];
				}
				
				$(this).animate({
					'backgroundPosition' : pos+' -31'
				},500)	
				break;
			case 2:
				$(this)
				.animate({
					opacity: 1
				},100,function(){
					$(this)
					.css("background-position-y", -32)
					.animate({
						opacity: 0
					},100)
				})
				break;
		}
	
		
	})
	
	//show footer
	if (typeof stop_fade_footer  == "undefined"){
		$('#footer').delay(1000).fadeIn(1000, function(){
			var params = { 
			allowScriptAccess: "always"
			, wmode : 'transparent' };
			var atts = { id: "myytplayer" };
			swfobject.embedSWF("http://www.youtube.com/e/b3sv7OmdUiE?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer-chromeless", "449", "251", "8", null, null, params, atts);
		});
	} else {
		$('#footer').show();
	}
	
	//#header-menu
	if(!$('#header-menu').hasClass('stop')){
		$('#header-menu').animate({
			'right' : 0
		}, 700
		,'easeOutBack')
	}
})

// resize index_banner div banner
function resizeBg(){	
	if(!$('#index_banner')) return !1;
	
	/* borwser width and height */
	var browser_width = $(window).width();
	var browser_height = $(document).height();
	
	
	if(browser_width > 1000){
		$('#index_banner').width(browser_width*1);
		
	} else {
		$('#index_banner').width(1000);
	}
	
	$('#index_banner').height(browser_height*1);
}

//resize borwser then resize msg box
$(window).resize(function() {
  resizeBg();
}).scroll(function () { 
  resizeBg();
});
