/*
    Document   : quizomat.js
    Created    : 29.04.2010
    Updated    : 27.08.2010
    Author     : Richard Lutz
    Description: JavaScript document for Quiz-O-Mat v3.2.1
*/

jQuery.noConflict();

jQuery(document).ready(function($) {

    /* QuizStart - Category Selector */

    /* Hide and Disable startups */
    $("div#quizStartMultiSelect").hide();
    $("form#quizStart input[type='image']").attr("disabled", "true");

    /* Endless Highliting */
    $("div#endless").click(function(){
        $("div#quizStartMultiSelect").show();
        $("div#turnament").removeClass('quizStartSelected');
        $("div#turnament").addClass('quizStartInactive');
        $("div#endless").addClass('quizStartSelected');
        $("input#quizMode").removeAttr('value');
        $("input#quizMode").attr('value', 'endless');
    });

    /* Turnament Highliting */
    $("div#turnament").click(function(){
        $("div#quizStartMultiSelect").show();
        $("div#endless").removeClass('quizStartSelected');
        $("div#endless").addClass('quizStartInactive');
        $("div#turnament").addClass('quizStartSelected');
        $("input#quizMode").removeAttr('value');
        $("input#quizMode").attr('value', 'turnament');
    });

    $("div.quizStartMultiEntry input[type='checkbox']").click(function(){
        // Count selected checkboxes
        var total   = $("div.quizStartMultiEntry input[type='checkbox']").length;
        var checked = $("div.quizStartMultiEntry input[type='checkbox']:checked").length;

        // Selected checkboxes between 1 and 4
        if (checked >= 1 && checked <= 4) {
               if ($('input#startQuizPlayerName').attr('value').length >= 3) {
                   $("form#quizStart input[type='image']").attr("src", "/images/button-start.gif");
                   $("form#quizStart input[type='image']").removeClass('noCursor');
                   $("form#quizStart input[type='image']").removeAttr("disabled");
               }
        } else {
           $("form#quizStart input[type='image']").attr("disabled", "true");
           $("form#quizStart input[type='image']").attr("src", "/images/button-start-disable.gif");
           $("form#quizStart input[type='image']").addClass('noCursor');
           alert('Bitte wählen Sie mindestens 1 und maximal 4 Kategorien aus.');
        }
    });
    $("input#startQuizPlayerName").keyup(function()
    {
        var checked = $("div.quizStartMultiEntry input[type='checkbox']:checked").length;

        if ($('input#startQuizPlayerName').attr('value').length >= 3 && $('#quizMode').attr('value').length >= 3 && checked >= 1) {
           $("form#quizStart input[type='image']").attr("src", "/images/button-start.gif");
           $("form#quizStart input[type='image']").removeClass('noCursor');
           $("form#quizStart input[type='image']").removeAttr("disabled");
        } else {
           $("form#quizStart input[type='image']").attr("disabled", "true");
           $("form#quizStart input[type='image']").attr("src", "/images/button-start-disable.gif");
           $("form#quizStart input[type='image']").addClass('noCursor');
        }
    });


    /**
     *  Challange
     */
    
    /* Check E-Mail Adress and display ok, nok class */
    $("input#challangeMailAdress1").keyup(function(){
        if(!validateEMail(document.getElementById('challangeMailAdress1'))) {
            $("input#challangeMailAdress1").removeClass('inputNok');
            $("input#challangeMailAdress1").addClass('inputOk smallMargin');
        } else if ($("input#challangeMailAdress1").val().length == 0) {
            $("input#challangeMailAdress1").removeClass('inputNok');
            $("input#challangeMailAdress1").addClass('smallMargin');
        } else {
            $("input#challangeMailAdress1").removeAttr('class');
            $("input#challangeMailAdress1").addClass('inputNok smallMargin');
        }
    });
    $("#challangeMailAdress2").keyup(function(){
        if(!validateEMail(document.getElementById('challangeMailAdress2'))) {
            $("input#challangeMailAdress2").removeClass('inputNok');
            $("input#challangeMailAdress2").addClass('inputOk smallMargin');
        } else if ($("input#challangeMailAdress2").val().length == 0) {
            $("input#challangeMailAdress2").removeClass('inputNok');
            $("input#challangeMailAdress2").addClass('smallMargin');
        } else {
            $("input#challangeMailAdress2").removeAttr('class');
            $("input#challangeMailAdress2").addClass('inputNok smallMargin');
        }
    });
    $("#challangeMailAdress3").keyup(function(){
        if(!validateEMail(document.getElementById('challangeMailAdress3'))) {
            $("input#challangeMailAdress3").removeClass('inputNok');
            $("input#challangeMailAdress3").addClass('inputOk smallMargin');
        } else if ($("input#challangeMailAdress3").val().length == 0) {
            $("input#challangeMailAdress3").removeClass('inputNok');
            $("input#challangeMailAdress3").addClass('smallMargin');
        } else {
            $("input#challangeMailAdress3").removeAttr('class');
            $("input#challangeMailAdress3").addClass('inputNok smallMargin');
        }
    });
    $("#challangeMailAdress4").keyup(function(){
        if(!validateEMail(document.getElementById('challangeMailAdress4'))) {
            $("input#challangeMailAdress4").removeClass('inputNok');
            $("input#challangeMailAdress4").addClass('inputOk smallMargin');
        } else if ($("input#challangeMailAdress4").val().length == 0) {
            $("input#challangeMailAdress4").removeClass('inputNok');
            $("input#challangeMailAdress4").addClass('smallMargin');
        } else {
            $("input#challangeMailAdress4").removeAttr('class');
            $("input#challangeMailAdress4").addClass('inputNok smallMargin');
        }
    });
    $("#challangeMailAdress5").keyup(function(){
        if(!validateEMail(document.getElementById('challangeMailAdress5'))) {
            $("input#challangeMailAdress5").removeClass('inputNok');
            $("input#challangeMailAdress5").addClass('inputOk smallMargin');
        } else if ($("input#challangeMailAdress5").val().length == 0) {
            $("input#challangeMailAdress5").removeClass('inputNok');
            $("input#challangeMailAdress5").addClass('smallMargin');
        } else {
            $("input#challangeMailAdress5").removeAttr('class');
            $("input#challangeMailAdress5").addClass('inputNok smallMargin');
        }
    });


   /**
    * New Member Form
    */
    $("#newMemberCountry").change(function() {
        // Get Data
        $.post("/inc/jquery2php.functions.php?a=getBL&cID=" + $("#newMemberCountry option:selected").attr('value'), function(data){
            $("#newMemberBundeslandDiv").html(data);
            if ($("#newMemberCountry option:selected").attr('value') != '') {
                $("#newMemberCountryInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
                $("#newMemberCountryInfo").removeAttr('style')
            }
		});
    });
    $("#nickname").change(function() {
        $.post("/inc/jquery2php.functions.php?a=checkUsername&name=" + $("#nickname").attr('value'), function(data){
            $("#newMemberNicknameInfo").html(data);
            $("#newMemberNicknameInfo").removeAttr('style');
		});
    });
    $("#name").change(function() {
        if ($("#name").attr('value').length >= 3) {
            $("#newMemberNameInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
            $("#newMemberNameInfo").removeAttr('style')
        }
    });
    $("#mail").change(function() {
        $.post("/inc/jquery2php.functions.php?a=checkMailAdr&mail=" + $("#mail").attr('value'), function(data){
            $("#newMemberMailInfo").html(data);
            $("#newMemberMailInfo").removeAttr('style');
            $("#newMemberMailInfo").removeClass('hidden');
            if (data.length == '0')
                $("#newMemberMailInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
		});
    });
    $("#url").change(function() {
        if ($("#url").attr('value').length >= 3) {
            $("#newMemberWebseiteInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
            $("#newMemberWebseiteInfo").removeAttr('style')
        }
    });
   $("#password1").keyup(function() {
        $.post("/inc/jquery2php.functions.php?a=checkPassword&pwd=" + $("#password1").attr('value'), function(data){
            $("#newMemberPasswordInfo").html(data);
            $("#newMemberPasswordInfo").removeAttr('style');
		});
    });
   $("#password2").keyup(function() {
        if ($("#password1").attr('value') == $("#password2").attr('value')) {
            $("#newMemberPasswordCheckInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok"> Passwort <span class="green">identisch</span>');
            $("#newMemberPasswordCheckInfo").removeAttr('style');
        } else {
            $("#newMemberPasswordCheckInfo").html('<img src="/images/icons/icon-nok.gif" alt="nok"> Passwort <span class="red">nicht identisch</span>');
            $("#newMemberPasswordCheckInfo").removeAttr('style');
        }
    });
    $("#day").change(function()     {checkBirthDate();});
    $("#month").change(function()   {checkBirthDate();});
    $("#year").change(function()    {checkBirthDate();});

    /*
     * Start Challenge
     */
    $("li.switchChallenge").toggle(
        function () {
            $('#challengeTypeInput').attr('value', 'public');
            $('#challengeTypePublic').show("slow");
            $('#challengeTypeMail').hide("slow");
        },
        function () {
            $('#challengeTypeInput').attr('value', 'private');
            $('#challengeTypePublic').hide("slow");
            $('#challengeTypeMail').show("slow");
        }
    );

    // Show Tooltips
    // id=content, class=ShowToolTip
    $('.ShowToolTip').tooltip();

    // Init News
    if ($("#homeNewsContainer").length) {
        $('#homeNewsContainer').ticker();
    }
});

/**
 * Show/Hide Profile Page
 */
function showUserProfile(pID)
{
    // load file into div#userProfile
    jQuery('#userProfile').html('&nbsp;');
    jQuery('#userProfile').load('/member/showprofile.php?id='+pID);
    jQuery('#userProfile').show();
}
function hideUserProfile() {
        jQuery('#userProfile').html('&nbsp;');
        jQuery('#userProfile').hide();
}

/**
 * Form Validators
 */
function validateUsername()
{
    if (validateIsEmpty(document.quizStart.username)) 	{
        alert('Bitte geben Sie einen Spielernamen an.');
        document.quizStart.username.focus();
        return false;
	} else {
        jQuery('#userProfile').html('&nbsp;');
        jQuery("#userProfile").addClass('loading');
        jQuery('#userProfile').show()
    }
    return true;
}
function validateIsEmpty(field) {
	if (field.value.length <= 1 || field.value == null)
		return true;
	else
		return false;
}
function validateEMail(field) {
	validRegExp = /(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i;
	if (field.value.length == 0 || field.value == null || field.value.search(validRegExp) == -1)
		return true;
	else
		return false;
}
function checkField(pField, pWrite)
{
	if (pField.value == '')
	{
		pField.value=pWrite;
	}
}
function checkBundesland() {
    if (jQuery("#newMemberBundesland option:selected").attr('value') > 0) {
        jQuery("#newMemberBundeslandInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
        jQuery("#newMemberBundeslandInfo").removeAttr('style')
    }
}
function checkBirthDate() {
    if (jQuery("#day option:selected").attr('value') > 0 && jQuery("#month option:selected").attr('value') > 0 && jQuery("#year option:selected").attr('value') < 1997) {
        jQuery("#newMemberBirthDateInfo").html('<img src="/images/icons/icon-ok.gif" alt="ok">');
        jQuery("#newMemberBirthDateInfo").removeAttr('style')
    }
}
function openprofile(url){
	var popupX = (screen.width/2)-(520/2);
	var popupY = (screen.height/2)-(380/2);
	var pos = "left="+popupX+",top="+popupY;
	winpops=window.open(url,"","width=520,height=380," +pos)
}
/* New Member simple Check for Submit */
function validateNewMember() {
    if (jQuery("#name").attr('value').length > 2 && jQuery("#password1").attr('value').length > 5 && jQuery("#password2").attr('value').length > 5 && jQuery("#mail").attr('value').length > 5 && jQuery("#nickname").attr('value').length > 2 && jQuery('#agb').attr('checked')) {
        return true;
    } else {
        alert('Bitte füllen Sie alle Pflichtfelder korrekt aus\nund akzeptieren Sie die AGB.');
        return false;
    }
}
// Function to open a Popup
function openPopup(url, w, h) {
	var popupX	=	(screen.width / 2) - (w / 2);
	var popupY	=	(screen.height / 2) - (h / 2);
	winpops		=	window.open(url, "", "width=" + w + ", height=" + h + ", scrollbars, left=" + popupX + ", top=" + popupY);
}
/* Challenge start simple Check for Submit*/
function validateNewChallenge() {
    if (jQuery("#challengeTypeInput").attr('value') == 'private' && jQuery("input#challangeMailAdress1").attr('value').length < 5) {
        alert('Um eine private Challenge zu starten, müssen Sie mindestens eine Empfängeradresse angeben.');
        return false;  
    } else {
        var checked = jQuery("div.quizStartMultiEntry input[type='checkbox']:checked").length;

        if (checked >= 1 && checked <= 4) {
           return true;
        } else {
            alert('Bitte wählen Sie mindestens 1 und maximal 4 Kategorien aus.');
            return false;
        }
    }
}
function QM_jumpMenu(targ,selObj,restore){
  eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
  if (restore) selObj.selectedIndex=0;
}
/**
 *  jQuery Tooltip Plugin
 *  http://intekhabrizvi.wordpress.com/
 *
 *  Copyright (c) Intekhab A Rizvi
 *  Licensed under GPL licenses
 *
 *  Version: 3.2.2
 *  Dated : 24-Mar-2010
 */
(function() {
	jQuery.fn.tooltip = function(options){
		 var defaults = {
		    offsetX: 20,  //X Offset value
		    offsetY: 18,  //Y Offset value
		    fadeIn : '100', //Tooltip fadeIn speed, can use, slow, fast, number
		    fadeOut : '100',//Tooltip fadeOut speed, can use, slow, fast, number
		    dataAttr : 'data',	//Used when we create seprate div to hold your tooltip data, so plugin search div tage by using id 'data' and current href id on whome the mouse pointer is so if your href id is '_tooltip_1' then the div which hold that tooltips content should have id 'data_tooltip_1', if you change dataAttr from default then you need to build div tag with id 'current dataAttr _tooltip_1' without space
		    bordercolor: '#035F98', // tooltip border color
		    bgcolor: '#F2F8F5', //Tooltip background color
		    fontcolor : 'black', //Tooltip Font color
		    fontsize : '11px', // Tooltip font size
		    folderurl : 'NULL', // Folder url, where the tooltip's content file is placed, needed with forward slash in the last (/), or can be use as http://www.youwebsitename.com/foldername/ also.
		    filetype: 'txt', // tooltip's content files type, can be use html, txt
		    height: 'auto', // Tooltip's width
		    width : 'auto', //Tooltip's Height
		    cursor : 'default' // Mouse cursor
		   };
	var options = jQuery.extend(defaults, options);
	var $tooltip = jQuery('<div id="divToolTip"></div>');
	return this.each(function(){
			jQuery('body').append($tooltip);
			$tooltip.hide();

		var element = this;
		var id = jQuery(element).attr('id');
		var filename = options.folderurl + id + '.' + options.filetype;
		var dialog_id = '#divToolTip';

		jQuery(this).hover(function(e){

				if(options.folderurl != "NULL"){
					jQuery(dialog_id).load(filename);

				}else
				{
					if(jQuery('#'+options.dataAttr + '_' + id).length > 0){
						jQuery(dialog_id).html(jQuery('#'+ options.dataAttr + '_' + id).html());
						//$(dialog_id).html(size);
					}else{
						jQuery(dialog_id).html(id);
						//$(dialog_id).html(size);
					}
				}
				//assign css value to div
				jQuery(element).css({'cursor' : options.cursor});
				if(jQuery(document).width() / 2 < e.pageX){
					jQuery(dialog_id).css({
						'position' : 'absolute',
						'border' : '1px solid ' + options.bordercolor,
						'background-color' : options.bgcolor,
						'padding' : '4px',
						'top' : e.pageY + options.offsetY,
                        // e.pageX - jQuery(dialog_id).width() + options.offsetX,
						'left' :  e.pageX + options.offsetX,
						'color' : options.fontcolor,
						'font-size' : options.fontsize,
						'height' : options.height,
						'width' : options.width
					});
				}else{
					jQuery(dialog_id).css({
						'position' : 'absolute',
						'border' : '1px solid ' + options.bordercolor,
						'background-color' : options.bgcolor,
						'padding' : '4px',
						'top' : e.pageY + options.offsetY,
						'left' : e.pageX + options.offsetX,
						'color' : options.fontcolor,
						'font-size' : options.fontsize,
						'cursor' : options.cursor,
						'height' : options.height,
						'width' : options.width
					});
				}
				jQuery(dialog_id).stop(true, true).fadeIn(options.fadeIn);
					},function(){
				jQuery(dialog_id).stop(true, true).fadeOut(options.fadeOut);
					}).mousemove(function(e){
				if(jQuery(document).width() / 2 < e.pageX){
				jQuery(dialog_id).css({
					'top' : e.pageY + options.offsetY,
					'left' : e.pageX - jQuery(dialog_id).width(),
					'height' : options.height,
					'width' : options.width
					});
				}else{
					jQuery(dialog_id).css({
					'top' : e.pageY + options.offsetY,
					'left' : e.pageX + options.offsetX,
					'height' : options.height,
					'width' : options.width
					});
				}
			});
		});
	};
 })(jQuery);

jQuery(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 728;
  var slides = jQuery('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  jQuery('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
  .wrapAll('<div id="slideInner"></div>')
  // Float left to display horizontally, readjust .slides width
  .css({
    'float' : 'left',
    'width' : slideWidth
  });

  // Set #slideInner width equal to total width of all slides
  jQuery('#slideInner').css('width', slideWidth * numberOfSlides);

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  jQuery('.control')
    .bind('click', function(){
    // Determine new position
      currentPosition = (jQuery(this).attr('id')=='rightControl')
    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
      manageControls(currentPosition);
      // Move slideInner using margin-left
      jQuery('#slideInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
      });
    });

  // manageControls: Hides and shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ jQuery('#leftControl').hide() }
    else{ jQuery('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ jQuery('#rightControl').hide() }
    else{ jQuery('#rightControl').show() }
    }
  });
