// ZOOM FUNCTION
$(function() {
$(".items a").click(function() {

 var url = $(this).attr("href");
 var wrap = $(".product_main_image").fadeTo("medium", 0.5);
 var img = new Image();
 img.onload = function() {
 	wrap.fadeTo("fast", 1);
 	wrap.find("img").attr("src", url);
 	if(wrap.find("img").attr("width") > 533 ){
 		wrap.find("img").removeAttr("height");
 	}else{
 		wrap.find("img").removeAttr("width");
 	}
 };
 var url2 = $(this).attr("href").replace("_product_full_image", "");
 var wrap2 = $(".jqzoom");
 wrap2.attr("href", url2);
 
 img.src = url;
 
});

var options = {
	    zoomWidth: 250, //310
	    zoomHeight: 300, //327
        xOffset: 15,
        yOffset: 0,
        showEffect:'show',
        hideEffect:'fadeout',
        fadeoutSpeed: 'slow',
        zoomType: 'reverse',
        title: false,
        imageOpacity: 0.4
};
	$('.jqzoom').jqzoom(options);
});

// Registration Form Validation
function ValidateRegisterForm(){
	var first_name = $("#user_registration_first_name input");
	var last_name = $("#user_registration_last_name input");
	var $user_account_login;
	var $user_account_password;
	var $user_account_password_confirm;
	var $user_account_email;
	var $agreement;
	
	$("#user_registration_username_password_and_email").find(":input").each(function(index){
		if (index == 0) {user_account_login = $(this);}
		if (index == 1) {user_account_password = $(this);}
		if (index == 2) {user_account_password_confirm = $(this);}
		if (index == 3) {user_account_email = $(this);}
	});

	$("#user_registration_confirmation").find(":input:checkbox").each(function(index){
		agreement = $(this);
	});
	
	if(registrationLengthRestrictionFirstName(first_name, 2, 50)){
		if(registrationLengthRestrictionLastName(last_name, 2, 50)){
			if (registrationLengthRestrictionUsername(user_account_login, 4, 50)) {
				if(registrationLengthRestrictionPassword(user_account_password, 6, 50)){
					if(registrationEqualtyRestrictionPassword(user_account_password, user_account_password_confirm)) {
						if(registrationEmailValidator(user_account_email)){
							if(registrationConfirmationValidator(agreement)){
								return true;
							}
						}
					}
				}
			}
		}
	}
	return false;
}

function registrationLengthRestrictionFirstName(elem, min, max){
	var uInput = elem.val();
	if(uInput.length >= min && uInput.length <= max){
		document.getElementById('user_registration_first_name_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_first_name_error').innerHTML = 'Vennligst skriv minimum ' +min+ ' tegn.';
		elem.focus();
		return false;
	}
}
function registrationLengthRestrictionLastName(elem, min, max){
	var uInput = elem.val();
	if(uInput.length >= min && uInput.length <= max){
		document.getElementById('user_registration_last_name_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_last_name_error').innerHTML = 'Vennligst skriv minimum ' +min+ ' tegn.';
		elem.focus();
		return false;
	}
}
function registrationLengthRestrictionUsername(elem, min, max){
	var uInput = elem.val();
	if(uInput.length >= min && uInput.length <= max){
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = 'Brukernavn må være minimum ' +min+ ' tegn.';
		elem.focus();
		return false;
	}
}
function registrationLengthRestrictionPassword(elem, min, max){
	var uInput = elem.val();
	if(uInput.length >= min && uInput.length <= max){
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = 'Passord må minimum være ' +min+ ' tegn.';
		elem.focus();
		return false;
	}
}
function registrationEqualtyRestrictionPassword(elem1, elem2){
	var uInput1 = elem1.val();
	var uInput2 = elem2.val();
	if(uInput1 == uInput2){
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = 'Passordene må være like.';
		elem1.focus();
		return false;
	}
}
function registrationEmailValidator(elem){
	var regexp = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";
	var regex = new RegExp( regexp );
	if (regex.exec( elem.val() )!=null){
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_username_password_and_email_error').innerHTML = 'Vennligst skriv inn en gyldig epost-adresse.';
		elem.focus();
		return false;
	}
}
function registrationConfirmationValidator(elem){
	if (elem.is(':checked')) {
		document.getElementById('user_registration_confirmation_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('user_registration_confirmation_error').innerHTML = 'Vennligst les vilkår og huk av boksen.';
		elem.focus();
		return false;
	}
}

// COMPETITION BUTTON CLICK EVENTS
$(function() { 
    $("#btn1").click(function() { 
        $("#valg1").slideDown();
        $("#valg2").slideUp();
        $("#valg3").slideUp();
        $('.survey input').attr("value",'Nei');
        
    }); 
        $("#btn2").click(function() { 
        $("#valg2").slideDown();
        $("#valg1").slideUp();
        $("#valg3").slideUp();
        $('.survey input').attr("value",'Ja');
    });
        $("#btn3").click(function() { 
        $("#valg3").slideDown();
        $("#valg1").slideUp();
        $("#valg2").slideUp();
        $('.survey input').attr("value",'Vet ikke');
    });
});

// FORM VALIDATOR
function formValidator(){
	if(document.getElementById('survey').value == 'Nei')
	{

		var zip = document.getElementById('zip');
		var comment = document.getElementById('comment');
		var email = document.getElementById('email');
		var terms = document.getElementById('terms');
		
		if(lengthRestriction(comment, 75, 1000)){
			if(emailValidator(email, "Vennligst skriv inn en gyldig epost-adresse.")){
				if(isNumeric(zip, "Vennligst skriv inn et gyldig postnummer.")){
					if(checkboxValidator(terms, "Vennligst les vilkår og huk av boksen.")){
						return true;
					}
				}
			}
		}
		return false;
	}
	return true;
}

function checkboxValidator(elem, helperMsg){
	if(document.getElementById('terms').checked == true){
		document.getElementById('checkbox_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('checkbox_error').innerHTML = helperMsg;
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		document.getElementById('email_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('email_error').innerHTML = helperMsg;
		elem.focus();
		return false;
	}
}
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression) && elem.value.length == 4){
		document.getElementById('zip_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('zip_error').innerHTML = helperMsg;
		elem.focus();
		return false;
	}
}
function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		document.getElementById('comment_error').innerHTML = '';
		return true;
	}else{
		document.getElementById('comment_error').innerHTML = 'Vennligst skriv minimum' +min+ ' tegn.';
		elem.focus();
		return false;
	}
}

// SCROLLABLE
$(document).ready(function() {
	$("#chained").scrollable({hoverClass: 'hover', size: 5}).navigator().mousewheel();	
});

// TOOLTIP
$(document).ready(function() { 
    $("#demo img[title]").tooltip({tip:'#demotip',effect:'slide',offset:[5,0]}); 
});

// LOGIN
$(document).ready(function() {
	$("#loginWrapper").hide();
	
	$("#loginBtn").click(function() { 
        $("#loginWrapper").slideToggle();
        $("#registerDrop").slideUp();
    }); 
	
	$("#loginButton").click(function(){
		//$("#loginArea").load('/pierrerobert/index.php/pierre/user/login');
		//$("#loginArea").post("/kernal/user/login.php", function(data){
			//alert("Data Loaded: " + data);
		 //});
		
		//$.post("/pierrerobert/index.php/pierre/user/login", $("#loginform").serialize(), function(data){
			//	alert("Data: + " data);
				//});
				
		$.post("/user/login", $("#loginform").serialize(),
				   function(data){
						var x = jQuery(data).find("div.warning");
						$("#warning").html(x);
						if(x.length == 0){
							location.reload();
						}
				   });
	});
	
//	var uid = $("#loginArea span span").html();
//	$(".ezcom-view-comment").each(function(index){
//		var commentid = $(".ezcom-view-comment").attr("rel");
//		var c = jQuery.trim(commentid);
//		var v = jQuery.trim(uid);
//		if(c==v){
//			$(this).find(".ezcom-comment-time").hide();
//			
//		}
//		else
//		{
//			//$(this).find(".ezcom-comment-tool").hide(); // check this function
//			alert("fdsfsd");
//		}
//	});
	
	
	

	
	//$(".ezcom-comment-author p:contains('Kong Harald')").hide("ezcom-view-comment");
	//	alert(uid);
	//	alert($(".ezcom-comment-author p").html());

	
});
// REGISTER
$(document).ready(function() { 
	//$("#registerDrop").hide();
	
	//$("#registerDrop").load('/pierrerobert/index.php/pierre/user/register #content', function(){
	//	$("input#redirect").attr("value", window.location.href);
	//});
	
	//	$("#registerDrop").load('/pierrerobert/index.php/pierre/user/register #content', function(){
		$("#regSubmit").click(function(){
			if(!ValidateRegisterForm()) {
				return false;
			}
			var referrer = document.referrer;
			$("input#redirect").attr("value", referrer);
		});
//			$.post("/pierrerobert/index.php/pierre/user/register", $("#registerform").serialize(),
//					   function(data){
//							alert("Data Loaded: " + data);
//							var x = jQuery(data).find("div.warning");
//							$("#regWarning").html(x);
//							if(x.length == 0){
//								location.reload();
//							}
//					   });
//		});
//	});
	
//	$("#registerBtn").click(function() { 
//		window.location.replace("/user/register");
//		$("#registerDrop").slideToggle("slow");
//		$("#loginWrapper").slideUp();
//	 });
	
	/* LOGOUT BUTTON */
	$("#logoutBtn").click(function() { 
		window.location.replace("/user/logout");
	 });
	
	$(".required_competition_text").hide();
	$(".info input:checkbox").change(function(){
		$(".required_competition_text").slideToggle();
	});
//	if($(".info input:checked")){
//		$(".required_competition_text").slideDown();
//	}else{
//		$(".required_competition_text").slideUp();
//	}
		
	
	
});

// OVERLAY 
$(document).ready(function() { 
	var triggers = $("img[rel]").overlay({ 
    expose: { 
        color: '#333', 
        loadSpeed: 200, 
        opacity: 0.9 
    }, 
    closeOnClick: false 
});	
});


$(document).ready(function() { 
	var triggers = $("input[rel]").overlay({ 
    expose: { 
        color: '#333', 
        loadSpeed: 200, 
        opacity: 0.9 
    }, 
    closeOnClick: false 
});	
});

// NEWSTICKER
$(document).ready(function() { 
	var first = 0;
	var speed = 1200;
	var pause = 10000;
	
//	function removeFirst(){
//		first = $("#newsticker ul li:first").html();
//		$("#newsticker ul li:first")
//		.animate({opacity:0}, speed)
//		.fadeOut('slow', function(){$(this).remove();});
//		addLast(first);
//	}
	
	function removeFirst(){
		first = $("#newsticker ul li:first").html();
		$("#newsticker ul li:first").animate({opacity:0}, speed);
		$("#newsticker ul li:first").slideUp('slow', function(){$(this).remove();});
		addLast(first);
	}
	
	function addLast(first){
		last = '<li style="display:none">'+first+'</li>'; 
		$("#newsticker ul").append(last);
		$("#newsticker ul li:last").slideDown();
	}
	
//	function addLast(first){
//		last = '<li style="display:none">'+first+'</li>'; 
//		$("#newsticker ul").append(last);
//		$("#newsticker ul li:last")
//		.animate({opacity:1}, speed)
//		.fadeIn('slow');
//	}
	
	interval = setInterval(removeFirst, pause);
	
});






