/**
 * Öffnet ein PopUp-Fenster und holt dieses in den Vordergrund
 */
function openPopup(url, title, settings) {
	var popup = window.open(url, title, settings);
	popup.focus();
}

/**
 * Löscht den Default-Wert einer Inputbox
 */
function clearText(field) {
	if (field.defaultValue == field.value) {
		field.value = "";
	}
}

/**
 * Stellt den Default-Wert einer Inputbox wieder her
 */
function resetText(field) {
	if (field.value == '') {
		field.value = field.defaultValue;
	}
}

/**
 * Stellt den übergebenen Wert einer Inputbox wieder her
 */
function resetText2(field, value) {
	field.value = value;
}


/**
 * Öffnet den Studio-Finder in einem PopUp-Fenster
 *
 * Die Inhalte der Felder PLZ und Ort werden als Parameter übergeben
 * Default-Werte werden vorher entfernt
 */
function openStudioFinder() {
	var url = 'studiofinder.php';
	var location = document.studiofinder.location.value;
	var country = document.studiofinder.country.value;

	if (location == 'PLZ oder Ort') {
		location = '';
	}

	resetText(document.studiofinder.location);

	url = url + '?location=' + location + '&country=' + country;
	openPopup(url, 'Studiofinder', 'width=627,height=653,status=yes,scrollbars=no,resizable=no');
}


/**
 * Öffnet den Studio-Finder in einem PopUp-Fenster
 *
 * Die Inhalte der Felder PLZ und Ort werden als Parameter übergeben
 * Default-Werte werden vorher entfernt
 */
function openTantasticFinder() {
	var url = 'studiofinder_tantastic.php';
	var location = document.studiofinderTantastic.location.value;
	var country = document.studiofinderTantastic.country.value;

	if (location == 'PLZ oder Ort') {
		location = '';
	}

	resetText(document.studiofinderTantastic.location);

	url = url + '?location=' + location + '&country=' + country + '&filter=tantastic';
	openPopup(url, 'Studiofinder', 'width=627,height=653,status=yes,scrollbars=no,resizable=no');
}

function openStudioFinderFlash(country, location, filter) {
	var url = 'studiofinder_tantastic.php';

	url = url + '?location=' + location + '&country=' + country + '&filter=' + filter;
	openPopup(url, 'Studiofinder', 'width=627,height=653,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Kontaktformular in einem PopUp-Fenster
 */
function openContactForm(stu_id) {
	var add = "";
	if(stu_id)
	{
		add = "&stu_id="+stu_id;
	}
	openPopup('/Kontakt/index.htm?id=1'+add, 'Kontaktformular', 'width=628,height=321,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Kontaktformular für Presseanfragen in einem PopUp-Fenster
 */
function openPressContactForm() {
	openPopup('Presse/index.htm?id=2', 'Presseanfrage', 'width=628,height=321,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Kontaktformular für Partnerschaften in einem PopUp-Fenster
 */
function openPartnerContactForm() {
	openPopup('Partner-werden/index.htm', 'Bewerbungsformular', 'width=627,height=652,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet Gewinnspiel als Pop-up
 */
function openGewinnspiel() {
	openPopup('http://gewinnspiel.sunpoint.de/', 'Gewinnspiel', 'width=930,height=635,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Newsletterformular für Newsletter in einem PopUp-Fenster
 */
function openNewsletterForm(target, email) {
	var queryString = email != '' && email != 'E-Mail-Adresse' ? '?email=' + urlencode(email) : '';
	openPopup(target + queryString, 'Newsletterformular', 'width=627,height=652,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Jobformular für Newsletter in einem PopUp-Fenster
 */
function openJobsForm() {
	openPopup('form_bewerbung.php', 'Jobformular', 'width=627,height=652,status=yes,scrollbars=no,resizable=yes');
}

/**
 * Öffnet das Kontaktformular für offene Fragen zur Expansion in einem PopUp-Fenster
 */
function openExpansionContactForm() {
	openPopup('Kontakt/index.htm?id=3', 'Kontaktformular', 'width=627,height=321,status=yes,scrollbars=no,resizable=no');
}

/**
 * Öffnet das Kontaktformular für Immobilienangebote in einem PopUp-Fenster
 */
function openExpansionForm() {
	openPopup('Expandieren/index.htm', 'Immobilienangebot', 'width=627,height=652,status=yes,scrollbars=no,resizable=no');
}
/**
 * Öffnet den T-Shirt Design Wettbewerb in einem PopUp-Fenster
 */
function openShirtDesign() {
	openPopup('http://gewinnspiel.sunpoint.de/popup.php', 'Wettbewerb', 'width=930,height=635,status=yes,scrollbars=no,resizable=no');
}
/**
 * Öffnet den Hauttypentest in einem PopUp-Fenster
 */
function openSkinTypeForm() {	
	var params = 'width=776,height=751,scrollbars=no,';
	
	if (screen.height <= 800) {
		var params = 'width=796,height=480,scrollbars=yes,';
	} 
	
	openPopup('Hauttypen-Test/index.htm', 'Hauttypentest', params + 'status=yes,resizable=no,location=no');
}

/**
 * Resettet beim Partner-Formualr den Value für Branche bei ausgewähltem Nein
 */
function checkSelfemployed()
{
	if(document.getElementById("selbstaendig-nein").checked)
	{
		document.getElementById("selbstaendig-ja-text").value="";
	}
}

function show(id){
	document.getElementById(id).style.display = "block";
}

function showLottery() {
	$(".open-popup").fancybox({
        frameWidth: 700,
        frameHeight: 567,
        overlayShow: false,
        overlayOpacity: 0.4
    }).trigger('click');	
}

function showPopup(id){
	document.getElementById(id).style.display = "block";
}

function closePopup(id){
	document.getElementById(id).style.display = "none";
}

// URL encode for newsletter form
function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

/*
 * Cookie tools
 */

var Cookie = {};

Cookie.domain = document.domain.substring(document.domain.indexOf('.') + 1, document.domain.length);

Cookie.createCookie = function(name, value) {
    document.cookie = name + '=' + value + ';path=/';
}

Cookie.readCookie = function(name) {
    name += '=';
    var snippets = document.cookie.split(';');
    for(var i = 0; i < snippets.length; i++) {
        var c = snippets[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return '';
}

Cookie.eraseCookie = function(name) {
    Cookie.createCookie(name, '');
}

/*
 * Device tools
 */

var Device = {};

Device.isMobile = function() {
  var mobileUserAgent = navigator.userAgent.toLowerCase();
  if (mobileUserAgent.search(/(iphone|ipod|opera mini|palm|blackberry|android|symbian|series60|acer|alcatel|audiovox|cdm|ericsson|lg\b|lge|motorola|mot|nec|nokia|panasonic|qci|sagem|samsung|sec|sanyo|sendo|sharp|sie-|sonyericsson|telit|telit_mobile_terminals|tsm)/) > -1) {
    return true;
  } else {
    return false;
  }
}

Device.selectSite = function() {
  if (Device.isMobile()) {
    if (Cookie.readCookie('useMobile') != 'false')
    {
      if (Cookie.readCookie('useMobile') == '') {
        var redirectToMobile = confirm('Möchten Sie die mobile SUNPOINT Studiosuche verwenden?');
        if (redirectToMobile) {
          Device.redirectToMobile();
        } else {
          Cookie.createCookie('useMobile', 'false');
        }
      } else if (Cookie.readCookie('useMobile') == 'true') {
        Device.redirectToMobile();
      }
    }
  }
}

Device.redirectToMobile = function() {
  Cookie.createCookie('useMobile', 'true');
  window.location = 'http://mobil.sunpoint.de';
}

Device.redirectToClassic = function() {
  Cookie.createCookie('useMobile', 'false');
  window.location = '/index.php';
}

function getParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

$(document).ready(function() {
  $('#force-mobile').click(function() {
    Device.redirectToMobile();
  });

  if (getParam("redirect") == 'classic')
  {
    Device.redirectToClassic();
  }
  else
  {
    Device.selectSite();
  }

  if (Device.isMobile()) $('#mobil-suchen').show();
});

