/* 
 * Netlash Image Randomizer : Random headeranimation for the Shops!
 *
 * @author Bert Pattyn <bert@netlash.com>
 */

function serialize( mixed_value ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
 
    var _getType = function( inp ) {
        var type = typeof inp, match;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function" && ktype == "object") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}


jQuery(document).ready(function($){
	
	// get current position
	var bgPosition = $('#header').css('background-position');
	
	// hack nodig voor IE
    if (bgPosition == 'undefined' || bgPosition == null) {
    	var pxCurrent = parseInt($('#header').css('background-position-x')); //die in hell
    } else {
    	var pxCurrent = parseInt(bgPosition.split('px')[0]);
    }

	// generate random movement value between 0 and 50
	var movementpx = Math.floor(Math.random() * 120) + 10;
	
	// generate left or right
	var movement = 'left';
	if(Math.floor(Math.random() * 2) == 1) movement = 'right';
	
	// generate new px value
	var pxNew = pxCurrent - movementpx;
	if(movement == 'right') pxNew = pxCurrent + movementpx;
	
	// begrenzers
	if(pxNew < 0) pxNew = pxCurrent + movementpx;
	if(pxNew > 832) pxNew = pxCurrent - movementpx;
	
	// set serialized cookie
	$.cookie('imagepx', serialize(pxNew), { expires: 366 });
	
	// animation
	$('#header')
			.css({backgroundPosition: pxCurrent + "px bottom"})
			.animate(
					{backgroundPosition: pxNew +'px bottom'},
					{duration: 2000},
					'easeInBack'
			);

});
