/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


/**
 * Hider
 *
**/



$(document).ready(function() {
	
	// the div that will be hidden/shown
	var panel = $("#hide");
	//the button that will toggle the panel
	var button = $("#show a");
	// do you want the panel to start off collapsed or expanded?
	var initialState = "expanded"; // "expanded" OR "collapsed"
	// the class added when the panel is hidden
	var activeClass = "hidden";
	// the text of the button when the panel's expanded
	var visibleText = "Kommentare ausblenden";
	// the text of the button when the panel's collapsed
	var hiddenText = "Kommentare einblenden";
	
	//---------------------------
	// don't    edit    below    this    line,
	// unless you really know what you're doing
	//---------------------------

	
	if($.cookie("panelState") == undefined) {
		$.cookie("panelState", initialState, { path: '/', expires: 70 });
	} 
	
	var state = $.cookie("panelState");
	
	if(state == "collapsed") {
		panel.hide();
		button.text(hiddenText);
		button.addClass(activeClass);
	}
   
	button.click(function(){
		if($.cookie("panelState") == "expanded") {
			$.cookie("panelState", "collapsed", { path: '/', expires: 70 });
			button.text(hiddenText);
			button.addClass(activeClass);
		} else {
			$.cookie("panelState", "expanded", { path: '/', expires: 70 });
			button.text(visibleText);
			button.removeClass(activeClass);
		}
		
		panel.slideToggle("slow");
		
		return false;
	});
});






// Blend 2.1 for jQuery 1.3+
// Copyright (c) 2010 Jack Moore - jack@colorpowered.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
(function(a,b){a.fn.blend=function(f,c){var e=this,d=["backgroundColor","backgroundImage","backgroundRepeat","backgroundAttachment","backgroundPosition","backgroundPositionX","backgroundPositionY","paddingTop","paddingLeft","paddingRight","paddingBottom","width","height"];f=parseInt(f,10)||a.fn.blend.speed;c=c||a.fn.blend.callback;e[0]&&!e.find(".jsblend")[0]&&!(a.browser.mozilla&&parseFloat(a.browser.version)<1.9)&&e.each(function(){var g='<span style="position:absolute;top:0;left:0;display:block"/>',h=this,e,k=h.currentStyle||b.getComputedStyle(h,null),i=a(g),j=a(g);if(h.style.position!=="absolute")h.style.position="relative";for(e=0;d[e];e+=1)if(k[d[e]])j[0].style[d[e]]=i[0].style[d[e]]=k[d[e]];i[0].style.backgroundImage=i[0].style.backgroundColor="";a(h).wrapInner(i).addClass("hover").prepend(j);a(h).bind("mouseenter mouseleave",function(a){a.type==="mouseenter"?j.stop().fadeTo(f,0,function(){c&&typeof c==="function"&&c()}):j.stop().fadeTo(f,1)})});return e};a.fn.blend.speed=400;a.fn.blend.callback=null})(jQuery,this)




$(document).ready(function(){
var runningRequest = false;
    var request;

    $('input#q').keyup(function(e){
        e.preventDefault();
        var $q = $(this);

        if($q.val() == ''){
            $('div#results').html('');
            return false;
        }

        //Abort opened requests to speed it up
        if(runningRequest){
            request.abort();
        }

        runningRequest=true;


// Der Pfad zum Suchscript

        request = $.getJSON("http://www.iphone-ticker.de/wp-content/themes/iphone-ticker_v2/__assets/scripts/search.php",{
            q:$q.val()
        },function(data){           
            showResults(data,$q.val());
            runningRequest=false;
        });

function showResults(data, highlight){
            var resultHtml = '';
            $.each(data, function(i,item){
                resultHtml+='<div class="result">';
                resultHtml+='<div class="icon">';
                resultHtml+='<a class="apptitle" href="http://www.ifun.de/app/'+item.title+'"><img src="'+item.kleine+'"></a>';
                resultHtml+='</div>';
                resultHtml+='<a class="apptitle" href="http://www.ifun.de/app/'+item.title+'">'+item.content+'</a>';
 				resultHtml+='<br><span class="light" style="font-size: 9px;">v'+item.ausgabe+'</span><br/>'+item.eur+'';
 				resultHtml+=''+item.preis+'</div>';
                resultHtml+='</div>';
            });

            $('div#results').html(resultHtml);
        }

        $('form').submit(function(e){
            e.preventDefault();
        });
    });
 });




 $(document).ready(function(){
     
     $("#navigation_buttons a").blend({speed:800});
     
 });




// setTimeout holder for the loading dots (...)
var demLoading;

function dem_Vote(that)
{
	inpts = that.getElementsByTagName('input');
	user_added = false;
	ans = -1;
	theSubmit = false;
	for (i = 0; i < inpts.length; i++)
	{
		cur = inpts[i];
		if (cur.type == 'radio' && cur.checked)
		{
			ans = cur.value;
			if (ans == 'newAnswer')
			{
			    user_added = true;
                ans = inpts[i+1].value;
            }
        }
		if (cur.name == 'dem_poll_id')
			poll_id = cur.value;

        if (cur.name == 'dem_cookie_days')
            cdays = cur.value;

		if (cur.type == 'submit')
			theSubmit = cur;

	}	

	// they haven't checked a box or they added a blank answer
	if (ans == -1 || ans == '')
		return false;

	demLoading = setTimeout(dem_loadingDots.bind(theSubmit), 50);

	path = that.action;

	if (user_added)
	{
	   path += "?dem_action=add_answer";
	   path += "&dem_new_answer="+encodeURIComponent(ans);

	} else
	{
	   path += "?dem_action=vote";
	   path += "&dem_poll_"+poll_id+"="+ans;
    } 

	path += "&dem_poll_id="+poll_id;
	path += "&dem_ajax=true";

	dem_ajax.open("GET", path, true);
	dem_ajax.onreadystatechange = dem_displayVotes.bind(that);
	dem_ajax.send(null);


	return false;
}

function dem_addUncheck()
{
	oUL = this.parentNode.parentNode;
	lis = oUL.getElementsByTagName('li');

	els = lis[lis.length-1].childNodes;

	for (i = els.length-1; i >= 0; i--)
		if (els[i].nodeName.toLowerCase() == 'a')
			els[i].style.display = '';
		else
			els[i].parentNode.removeChild(els[i]);



	Inp = oUL.getElementsByTagName('input');
    for (i = 0; i < Inp.length; i++)
    {
        Inp[i].onclick = function () { return true };
    }

    return true;
}

function dem_addAnswer(that)
{
    allBoxes = that.parentNode.parentNode.getElementsByTagName('input');

    for (i = 0; i < allBoxes.length; i++)
    {
        allBoxes[i].onclick = dem_addUncheck;
		allBoxes[i].checked = false;
    }

	that.style.display = 'none';
	i1 = document.createElement('input');
	i1.type = 'radio';
	i1.value = 'newAnswer';
	i1.checked = true;

	i2 = document.createElement('input');
	i2.className = 'addAnswerText';

	that.parentNode.appendChild(i1);
	that.parentNode.appendChild(i2);	

	i2.focus();

    return false;
}

// very simple ajaxy loading visual
// adds 3 dots to link, then erase and start over
function dem_loadingDots() {

	isInput = this.nodeName.toLowerCase() == 'input';

	str = (isInput) ? this.value : this.innerHTML;

	if (str.substring(str.length-3) == '...')
		if (isInput)
			this.value     = str.substring(0, str.length-3);
		else
			this.innerHTML = str.substring(0, str.length-3);
	else
		if (isInput)
			this.value     += '.';
		else
			this.innerHTML += '.';

	demLoading = setTimeout(dem_loadingDots.bind(this), 200);
}

function dem_clearDots() {
	clearTimeout(demLoading);
}


function dem_getVotes(path, that)
{

	that.blur();
	demLoading = setTimeout(dem_loadingDots.bind(that), 50);

	dem_ajax.open("GET", path, true);
	dem_ajax.onreadystatechange = dem_displayVotes.bind(that.parentNode);
	dem_ajax.send(null);

    return false;
}

function dem_displayVotes ()
{

	if (dem_ajax.readyState != 4)
		return false;

	if (dem_ajax.status != 200)
	{
		alert('Error '+dem_ajax.status);
		return false;
	}

	clearTimeout(demLoading);
	this.innerHTML = dem_ajax.responseText;
}

function dem_getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


dem_ajax = new dem_getHTTPObject();


/*  from prototype.js */
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}
