'use strict';

// Disable Right-Click
//$(document).bind('contextmenu', false);

// Search Text
(function () {
    var $q = $('#search_q'),
        offset = $q[0].offsetHeight - 34,
        method = function (e) {
            var val = $q.attr('value'),
                focus = e.type === 'focus';
            
            if (focus && val === 'SEARCH') {
                $q.removeClass('default').attr('value', '');
            } else if (!focus && !val) {
                $q.addClass('default').attr('value', 'SEARCH');
            }
        };
    
    $q.height($q.height() - offset);
    $q.addClass('default').focus(method).blur(method);
}());

// Showcase Sliders
(function ($) {
    var $showcases = $('.sc_container').removeClass('jsless'),
        duration = 750,
        $current,
        
        closed = function () {
            $(this).parent().data('closing', false);
        },
        animateone = function ($elem, name, prop, goal, method) {
            var anim = {};
            anim[prop] = goal;
            method = method ? closed : $.noop;
            $elem.find(name).stop(true).animate(anim, duration, method);
        },
        animateboth = function ($elem, opening) {
            var opacity = opening ? 1 : 0,
                height = opening ? 'start' : 'end';
            
            animateone($elem, '.sc_text', 'opacity', opacity);
            animateone($elem, '.sc_mask', 'height', $elem.data(height), !opening);
        },
        
        checkem = function () {
            var $this = $(this);
            if ($current !== $this) {
                $current = $this.data('closing', false);
                changem();
            }
        },
        changem = function () {
            var i = $showcases.length,
                closem = [],
                $tmp;
            
            while (i) {
                i -= 1;
                $tmp = $($showcases[i]);
                if ($tmp !== $current && !$tmp.data('closing')) {
                    closem[closem.length] = $tmp;
                }
            }
            
            i = closem.length;
            while (i) {
                i -= 1;
                animateboth(closem[i]);
            }
            
            animateboth($current, true);
        },
        
        loadem = function (i, elem) {
            var $elem = $(elem),
                start = $elem.find('.sc_text')[0].offsetHeight;
            
            $elem.data({
                start: start,
                end: elem.offsetHeight,
                closing: true
            }).find('.sc_mask').height(start);
            
            animateboth($elem);
        };
    
    $(window).load(function () {
        if ($showcases.length) {
            $showcases.each(loadem);
            $showcases.parent().delegate('.sc_container', 'mouseover', checkem);
        }
    });
}($));

// Showcases - Lite
$('.sc_lite').removeClass('jsless').each(function (index, elem) {
    $this = $(this);
    $this.find('.sc_mask').height($this.find('.sc_text')[0].offsetHeight);
});

// Deep-Links
$(document).delegate('.deeplink', 'click', function () {
    window.location = $(this).find('a').attr('href');
});

// Fancy Preview Pages
(function ($) {
    var count = $('#previews_nav').find('li').length;
    count *= 110;
    $('#previews_nav_nav').find('ul').width(count);
    
    count = $(window).width();
    count -= (count - 990) / 2 + 240;
    $('#previews_nav_nav').width(count);
    $('#previews_nav_gd').width(count);
}($));

function createImgStuff(id) {
    $('#previews_full').html('<img src="' + rel + 'images/' + id + '_full.jpg" /><img id="art_wrapper" src="' + rel + 'images/wrapper.gif" />');
    $('#previews_others').load('../aias.php?id=' + id);
}

$(document).delegate('#previews_nav a', 'click', function (e) {
    var id = this.href.split('/');
    id = id[id.length - 1];
    if (!(id.indexOf('#') + 1)) {
        e.preventDefault();
        createImgStuff(id);
    }
});

$(function () {
    var hash = location.hash ? location.hash.substring(1) : '';
    if (hash && hash == parseInt(hash, 10)) {
        createImgStuff(hash);
    }
});

$('.chaching').append('<div class="chaching_lbl"><p>Sold</p></div>');
