// JavaScript Document

	function slideSwitch() {
		var $active = $('.home_slide img.active');
	
		if ( $active.length == 0 ) $active = $('.home_slide img:last');
	
		var $next =  $active.next().length ? $active.next()
			: $('.home_slide img:first');
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}
	
	$(document).ready(function() {
		
		var userAgent = navigator.userAgent.toLowerCase();
		var seacrhstring = userAgent.search(/msie/i);
		if(seacrhstring>0) {
			browser	= 0;
		} else {
			browser	= 1;
		}
		
// MENU
		$('ul.sf-menu').superfish();

// SLIDER
		$('#mycarousel').jcarousel({
			scroll:3,
			//wrap:'circular',
			animation: 'normal'
		});

// IMAGE TRANS
		var counting	= $('.home_slide img').length;
		if(counting > 1) {
			setInterval( "slideSwitch()", 5000 );
		}
		
// LOGIN
		$("#frm_login").validate({
			rules: {
				username: "required",
				password: "required"
			},
			messages: {
				username: " ",
				password: " "
			},
			errorPlacement: function(error, element) {
//				if ( element.is(":radio") )
//					error.appendTo( element.parent().next().next().next() );
//				else if ( element.is(":checkbox") )
//					error.appendTo ( element.next().next() );
//				else
//					error.appendTo( element.parent().next().next() );
			},
			submitHandler: function() {
				$.post($("#frm_login").attr('action'), { 
					username:$('#username').val(),
					password:$('#password').val()
				} ,function(data) {
					//alert(data);
					if(data==1) {
						//alert(data);
						$('#sendstat').empty().append('Login successful. Redirecting...');
						document.location = based+'admin-cp/home';
					} else {
						$('#sendstat').empty().append('<span>'+data+'</span>');
					}
				});
				return false;
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});
		
		$('.img').click(function() {
			$(this).each(function() {
				var url = $(this).attr('href');
				var href = $(jQuery('<a href="'+ url +'"></a>'));
				$("#big_image img").remove();
				$("#big_image").append(href);
				loadImages("#big_image");
			});		 
			return false;
		});
		
		$('.gallery_img').click(function() {
			$(this).each(function() {
				var url = $(this).attr('href');
				var href = $(jQuery('<a href="'+ url +'"></a>'));
				$("#gallery_big_image img").remove();
				$("#gallery_big_image").append(href);
				loadImages("#gallery_big_image");
			});		 
			return false;
		});
		
	})

	function loadImages(container) {
		var loadingpage = $(jQuery('<div id="loadingimg"></div>'));
		var image_src = $(container).find('a').attr('href');
		$(container).empty();
		
		$(container).append(loadingpage);
		loadingpage.fadeTo('fast',0.5);
		loadingpage.fadeIn('slow',function() {
			img = new Image();
			$(img).load(function() {
				$(this).hide();
				loadingpage.before(this);
				$(this).fadeIn(function() {
					loadingpage.fadeOut("slow",function() {
						loadingpage.remove();
					});
				});
			})
			.error(function() {
				alert('error');	
			})
			.attr('src', image_src);
		});
	}


