/**
* EIGENE JAVASCRIPT ROUTINEN (basierend auf jquery)
* und SKYPE BUTTON JS
*
*/


/* JS-Funktionen losfeuern wenn das Dokument bereit ist. */
$(document).ready( function()
{
   //IMGREP.rollover.init();

   //PRODUCTS.getId.popup();
   // table cart form
   //PRODUCTS.getId.init();
   //png fix
   //jQuery(function(){$('div#page').pngFix( { blankgif:'clear.gif' } );});
});


// ---------------------------------------------------------------------------------

/* HEADER LOGO MOUSEOVER  (image replacement mit bild suffix _ro bei allen img class="rollover") */
IMGREP = {};
IMGREP.rollover =
{
   init: function()
   {
      this.preload();
     
      jQuery("#header_image").hover(
         function () { $(this).attr( 'src', IMGREP.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', IMGREP.rollover.oldimage($(this).attr('src')) ); }
      );
      
   },

   preload: function()
   {
      jQuery(window).bind('load', function() {
         $('#header_image').each( function( key, elm ) { $('<img>').attr( 'src', IMGREP.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_ro' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_ro\./, '.');
   }
};


// ---------------------------------------------------------------------------------

/* (CLEAR) INPUT DEFAULT VALUES */
function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}
function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}



/** 
* SKYPE.JS für SKYPE BUTTON IM FOOTER
 */
var activex = ((navigator.userAgent.indexOf('Win')  != -1) && (navigator.userAgent.indexOf('MSIE') != -1) && (parseInt(navigator.appVersion) >= 4 ));
var CantDetect = ((navigator.userAgent.indexOf('Safari')  != -1) || (navigator.userAgent.indexOf('Opera')  != -1));

function oopsPopup() {
    if((navigator.language && navigator.language.indexOf("ja") != -1) || (navigator.systemLanguage && navigator.systemLanguage.indexOf("ja") != -1) || (navigator.userLanguage && navigator.userLanguage.indexOf("ja") != -1)) {
        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops_ja.html";
    } else {
        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops.html";
    }
	var windowName = "oops";
	var popW = 540, popH = 305;
	var scrollB = 'no';
	w = screen.availWidth;
	h = screen.availHeight;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	oopswindow = window.open(URLtoOpen, windowName,'width=' + popW + ',height=' + popH + ',scrollbars=' + scrollB + ',screenx=' +leftPos +',screeny=' +topPos +',top=' +topPos +',left=' +leftPos);
	return false;
}

if(typeof(detected) == "undefined" && activex) {
    document.write(
        ['<script language="VBscript">',
        'Function isSkypeInstalled()',
        'on error resume next',
        'Set oSkype = CreateObject("Skype.Detection")',
        'isSkypeInstalled = IsObject(oSkype)',
        'Set oSkype = nothing',
        'End Function',
        '</script>'].join("\n")
    );
}

function skypeCheck() {
    if(CantDetect) {
        return true;
    } else if(!activex) {
        var skypeMime = navigator.mimeTypes["application/x-skype"];
        detected = true;
        if(typeof(skypeMime) == "object") {
            return true;
        } else {
            return oopsPopup();
        }
    } else {
        if(isSkypeInstalled()) {
            detected = true;
            return true;
        }
    }
    
    detected = true;
    return oopsPopup();
}

function loadDetection() {
    if(document.getElementById && document.getElementsByTagName) {
        if (window.addEventListener) window.addEventListener('load', addDetection, false);
        else if (window.attachEvent) window.attachEvent('onload', addDetection);
    }
}

function addDetection() {
    var pageLinks = document.getElementsByTagName("a");
    for (var i=0; i < pageLinks.length; i++) {
        if(pageLinks[i].childNodes[0] && pageLinks[i].childNodes[0].src) {
            if((pageLinks[i].childNodes[0].src.indexOf('download.skype.com\/share\/skypebuttons') != -1 || pageLinks[i].childNodes[0].src.indexOf('mystatus.skype.com') != -1) && (typeof(pageLinks[i].onclick) == "undefined" || pageLinks[i].onclick == null)) {
                pageLinks[i].onclick = function sChk() { return skypeCheck(); }
            }
        }
    }
}

loadDetection();



/**
*
* AJAXSEARCH: Zeitverzögerung und Sucher erst ab drei Chars
*
*/

var test = null;

function Chars()
{
  var length = $('#searchterm').val().length;
  $("#countchars").val(length);      
}

function Taste(e)
{
  e.preventDefault();
  if (test != null)
    clearTimeout(test);
  if ($("#searchterm").val().length > 2) {
    test = setTimeout(Abfrage, 500);
  }
}

function Abfrage()
{
  if (test != null)
    clearTimeout(test);
	//$.ajax({ type: "POST", data: ci_token });	
    xajax_xajax_search($("#searchterm").val());
    
    //get the position of the placeholder element  
	var pos = $("#fe_xajaxsearch").offset();    
	var left = 593+(pos.left) + "px";
	var right = (pos.right) + "px";    
	var top = 26+pos.top + "px";

	//show  
	$("#fe_searchresult").css( { 
		left: left, 
		top: top
	} );

    $("#fe_searchresult").css("display", "block");      
  return false;
}




/**
*
* VERSCHIEDENE
*
*/

/* left padding of strings */
function leadingZeros(num, totalChars, padWith) {
    num = num + "";
    padWith = (padWith) ? padWith : "0";
    if (num.length < totalChars) {
        while (num.length < totalChars) {
            num = padWith + num;
        }
    } else { }

    if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded
        num = num.substring((num.length - totalChars), totalChars);
    } else {}

    return num;
}


/* ORDER: checks the radio button if corresponding tab is clicked  TODO: geht nich wg. den accessible tabs*/
function checkPaymentRadio(payment) {

    var $radios = $('input:radio[name=zahlungsart]');
    // if($radios.is(':checked') === false) {
        // $radios.filter('[value=vorkasse]').attr('checked', true);
    // }

    switch(payment) {
        case "vorkasse":
            $radios.filter('[value=vorkasse]').attr('checked', true);             
            break;
        case "bankeinzug":
            $radios.filter('[value=bankeinzug]').attr('checked', true);        
            break;
        case "visa":
            $radios.filter('[value=visa]').attr('checked', true);        
            break;
        case "rechnung":
            $radios.filter('[value=rechnung]').attr('checked', true);        
            break;            
        default:
            $radios.filter('[value=vorkasse]').attr('checked', true);          
            break;
    }
  return true;
}


//popup
function popup(zielurl,breite,hoehe){
   rest = "status=no,titlebar=no,scrollbars=no,resizable=no,width="+breite+",height="+hoehe+",left=100px,top=100px";
   modulmaker = window.open(zielurl,'popup',rest);
}

//number count
function numberChars (form, input_field, chars_field) {
  document.form.chars_field.value = document.form.input_field.value.length + 1;
  return true;
}

//gallery zoom
var zoom; 
if ((window.document.zoomBild) && (window.document.zoomBild.width <= 176)) 
  zoom = 2; 
else 
  zoom = 1.3;


  
  /**
*
* HIDE/SHOW
*
*/

//element (checkbox) zum anzeigen/verstecken und zu versteckendes div
function hide_show(element, div) {
  //Hide div w/id hidden_mobile
  $(div).css("display","none");

  //onclick handler to checkbox w/id show_hidden_mobile
    $(element).click(function(){    
      // If checked
      if ($(element).is(":checked"))
      {
          //show the hidden div
          $(div).show("fast");
      }
      else
      {     
          //otherwise, hide it
          $(div).hide("fast");
      }
  });
}

$(document).ready(function(){
	//mobile content für news/produkte im backend
	//hide_show("#show_hidden_mobile", "#hidden_mobile");
	//top-attribut (startseite) für news/produkte im backend
	//hide_show("#show_hidden_top", "#hidden_top");
});

