jQuery(document).ready(function() {
    PearlShell.init(1500);
    Tinter.init();
    var calcHeight = function() {
        var height = jQuery(window).height();
        if (height < 800) {
            height_ = 800;
        } else {
            height_ = height - 100;
        }
        jQuery('#main-content').css('min-height', height_);        
    }
    calcHeight();
    jQuery(window).resize(function() {
        calcHeight();
    });
    /*if (jQuery('#images_container')) {
        var autoSlide = function() {
            jQuery('#images_container img:visible').fadeOut('slow', function() {
                var next = jQuery(this).next();                
                if (!next.length)
                    next = jQuery('#images_container img:first');
                next.fadeIn('slow');
            });
        }
        setInterval(autoSlide, 7000);
    }*/
});


var PearlShell = {
    time : 1500,
    end : {
        x: 290,
        y: 310,
        angle: 30
    },
    pearl1 : {
        x: 100,
        y: 210,
        angle: 60,
        length: 0.9
    },
    pearl2 : {
        x: 280,
        y: 50,
        angle: 70,
        length: 0.2
    },
    pearl3 : {
        x: 500,
        y: 200,
        angle: 90,
        length: 0.2
    },
    close : function (id, callback) {
        var self = this;
        var bezier = new jQuery.path.bezier({
            start : self[id],
            end : self.end
        });
        if(jQuery.support.cssFloat) {
            jQuery('#'+id).stop();
        }
        jQuery('#'+id).animate({
            path : bezier, 
            opacity: 0, 
            'filter': ''
        }, self.time, function() {
            jQuery(this).hide();
            if(typeof callback == "function") {
                callback();
            }
        });
    },
    open : function (id, callback) {
        var self = this;
        jQuery('#'+id).show();
        var bezier = new jQuery.path.bezier({
            start : self.end,
            end : self[id]
        });
        if(jQuery.support.cssFloat) {
            jQuery('#'+id).stop();
        }
        jQuery('#'+id).animate({
            path : bezier, 
            opacity: 1, 
            'filter': ''
        }, self.time, function() {
            if(typeof callback == "function") {
                callback();
            }
        });
    },
    init : function (time) {
        var self = this;
        self.time = time;
        if(jQuery('#seashell').length == 0) {
            return;
        }
        var so = new Image();
        so.src = "wp-content/themes/ovaal2011/img/merekarp_lahti.png";
        var shellClosed = false;
        jQuery('#seashell').click(function() {
            if(shellClosed) {
                shellClosed = false;
                self.close('pearl1');
                self.close('pearl2');
                self.close('pearl3', function() {
                    jQuery('#seashell').css("background", "url('wp-content/themes/ovaal2011/img/merekarp_kinni.png') no-repeat center center");
                });
            } else {
                shellClosed = true;
                self.open('pearl1');
                self.open('pearl2');
                self.open('pearl3');
                jQuery(this).css("background", "url('wp-content/themes/ovaal2011/img/merekarp_lahti.png') no-repeat center center");
            }
        });
    }
};

var Tinter = {
    init : function() {
        var hue = jQuery.jStorage.get('ovaal-hue', "180");
        this.setHue(hue);
        jQuery('#color-slider').css('top', hue / 1.7734);

        if(jQuery('#color-slider').length == 0) {
            return;
        }
        var self = this;
        jQuery('#color-slider').draggable({
            axis: "y",
            containment: 'parent',
            cursor: 'move',
            scroll: false,
            drag: function(e, ui) {
                var offsetY = e.pageY - jQuery('#color-select').offset().top;
                self.setHue(Math.round(offsetY * 1.7734));
            }
        });
        jQuery('#color-select').mousedown(function(e) {
            var offsetY =  e.pageY - jQuery('#color-select').offset().top;
            self.setHue(Math.round(offsetY * 1.7734));
            jQuery('#color-slider').css('top', offsetY - 5);
        });
    },

    setHue : function(hue) {
        if(jQuery.browser.msie) {
            jQuery('#container').css({
                'background': 'transparent',
                '-ms-filter': '"progid:DXImageTransform.Microsoft.gradient(startColorstr=#'+
                this.parseHSLa(hue+',100%,50%,0.1')+',endColorstr=#'+
                this.parseHSLa(hue+',100%,50%,0.1')+')"',
                'filter': 'progid:DXImageTransform.Microsoft.gradient(startColorstr=#'+
                this.parseHSLa(hue+',100%,50%,0.1')+',endColorstr=#'+
                this.parseHSLa(hue+',100%,50%,0.1')+')',
                'zoom': '1'
            });
        } else {
            jQuery('#container').css('background-color', 'hsla('+hue+',100%, 50%, 0.1)');
        }
        jQuery.jStorage.set("ovaal-hue", hue);
    },

    hexvals : ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'],

    parseHSLa : function(hsla) {

        hsla = hsla.split(',');
        var h	= parseInt(hsla[0], 10),
        s	= parseInt(hsla[1], 10),
        l	= parseInt(hsla[2], 10),
        a	= hsla[3] || 1,
        current, temp1, temp2, r, g, b, filterValue = '';

        h = (h < 0) ? 0 : ((h > 360) ? 360 : h) / 360;
        s = (s < 0) ? 0 : ((s > 100) ? 100 : s) / 100;
        l = (l < 0) ? 0 : ((l > 100) ? 100 : l) / 100;

        if (s === 0) {
            for (var x = 0; x < 3; x++) {
                current = l * 255;
                filterValue += (this.hexvals[parseInt((current / 16), 10)] + this.hexvals[parseInt((current % 16), 10)]);
            }
        } else {

            temp2 = (l < 0.5) ? (l * (1 + s)) : (l + s - l * s);
            temp1 = 2 * l - temp2;

            r = this.hueToRGB(temp1, temp2, h + 1/3) * 255;
            g = this.hueToRGB(temp1, temp2, h) * 255;
            b = this.hueToRGB(temp1, temp2, h - 1/3) * 255;

            filterValue += (this.hexvals[parseInt((r / 16), 10)] + this.hexvals[parseInt((r % 16), 10)]);
            filterValue += (this.hexvals[parseInt((g / 16), 10)] + this.hexvals[parseInt((g % 16), 10)]);
            filterValue += (this.hexvals[parseInt((b / 16), 10)] + this.hexvals[parseInt((b % 16), 10)]);

        }
        filterValue = this.getAlpha(a) + filterValue;
        return filterValue;
    },

    hueToRGB : function(p, q, t) {
        if (t < 0) {
            t += 1;
        }
        if (t > 1) {
            t -= 1;
        }
        if (t < 1/6) {
            return p + (q - p) * 6 * t;
        }
        if (t < 1/2) {
            return q;
        }
        if (t < 2/3) {
            return p + (q - p) * (2/3 - t) * 6;
        }
        return p;
    },

    getAlpha : function(alpha) {
        alpha = parseFloat(alpha, 10);
        if (alpha < 0) {
            alpha = 0;
        } else if (alpha > 1) {
            alpha = 1;
        }
        alpha = alpha * 255;
        return (this.hexvals[parseInt((alpha / 16), 10)] + this.hexvals[parseInt((alpha % 16), 10)]);
    }
}
