var changestr = function(obj) {
    var k = obj.id;
    
    window.open('/view/changestr/' + k.replace(/\s/g, ''), 'TPCS', 'resizable=yes,scrollbars=no,status=no,width=441,height=450');
    
    return false;
};

var cmi = function(object, on, val) {
    if(!val) {
        val = ['.jpg', '_o.jpg'];
    }
    
    img = $(object).find('img');
    
    if(on) {
        img.attr('src', img.attr('src').replace(val[0], val[1]));
    } else {
        img.attr('src', img.attr('src').replace(val[1], val[0]));
    }
};

var slideID = function(id, removeID) {
    if(!removeID) {
        $("#"+removeID).hide();
    }
    
    $("#"+id).slideDown("slow");
};

var smbForm = function() {
    var options = { 
        type:       'post',
        url:        '/ajax/val_register_form',
        success:    function(a,b) { 
            if(a=='OK') {
                $('#rform').submit();
            }
            else { 
                alert(a); 
            }
        } 
    };

    $("#rform").ajaxSubmit(options);
};

$(document).ready(function() {
    var sure_delete_product = '';
    
    $.get('/languageview/getstring/sure_delete_product', function(data) {
        sure_delete_product = data;
    });
    
    $('.cproductoptions a.tellafriend').click(function() {
        var href = $(this).attr('href');
        
        window.open(href, 'TP-TaF', 
        'resizable=yes, scrollbars=no, status=no, width=441, height=450');
        
        $(this).blur();
        return false;
    });
    
    $('#carousel a').mouseover(function() {
        cmi(this, 1, ['.png', '_o.png']);
    });
    
    $('#carousel a').mouseout(function() {
        cmi(this, 0, ['.png', '_o.png']);
    });
    
    $('.btnlink').parent().click(function() {
        location.href=$(this).find('.btnlink').attr('href');
    });
    
    $('.btnlink').parent().mouseover(function() {
        $(this).toggleClass('on');
    });
    
    $('.btnlink').parent().mouseout(function() {
        $(this).toggleClass('on');
    });
    
    $('#rform input').keypress(function(e) {
        var key;
        
        if(window.event) {
            key = window.event.keyCode; //IE
        }
        else {
            key = e.which; //firefox
        }
        
        return (key != 13);
    });
    
    $('#rform #submit input').click(function() {
        smbForm();
        return false;
    });
    
    $('#basketoverview .delete').click(function() {
        return confirm(sure_delete_product);
    });
    
    $('#basketoverview .price select').change(function() {
        $('#basketoverview .price form').submit();
    });
    
    $('#movie_labels').change(function() {
        var label = $(this).find(':selected').val();
        
        $.get('/ajax/get_movie_character_list/' + label, function(data) {
            $('#movie_characters').html(data);
        });
    });
    
    var today = new Date();
    var year_range = (today.getFullYear()-100) + ':' + today.getFullYear();
    
    if($.datepicker) {
        $('#birthdayclub #birthday').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
            firstDay: 1,
            maxDate: '+0d',
            minDate: new Date(1900, 0, 0),
            yearRange: year_range
        });
    }
    
    $('#birthdayclub #interests .interest_image').click(function() {
        var checkbox = $(this).find('input[type="checkbox"]');
        
        if(checkbox.attr('checked') == true) {
            checkbox.attr('checked', '');
        } else {
            checkbox.attr('checked', 'checked');
        }
    });
    
    $(document).pngFix();
});