$(document).ready( function()
{
	$('#btn_entry_login').click(function() {
		var user = $('#username').val();
		var pass = $('#user_password').val();
		login(user, pass);
	});

	$('#btn_login').click( function() { 
		var src = $('#login_box');
		var dest = src.html();
		src.find('#quick_login').remove();
		$("#dialog_base .dlg_body:eq(0)").html(dest);

		dialog_config.element = '#dlg_container';
		dialog_config.onclose = 'var src=$("#dialog_base .dlg_body:eq(0)");var dest=src.html();src.html("");$("#login_box").html(dest);dest="";';
		dialog.setTitle('Вход');
		dialog.open();
	});

	init_tabs();
	$('span.vip_status').each(function() {
		$(this).parent().parent().addClass('vip');
	});
		
});

function init_tabs() {
	$('.tabset').each(function(){
		var tabset = $(this);
		tabset.find('.tab_header a').each( function(index) {
			$(this).click(function(evnt){
				if ( !$(this).hasClass('active') ) {
					tabset.find('.tab_header a').removeClass('active');
					$(this).addClass('active');
					var body = tabset.find('.tab_body:eq(0) .tab_item');
					body.removeClass('active_tab');
					$(body.get(index)).addClass('active_tab');
				}
			})
		})
	})	
}

function close_dialog() { dialog.close(); }
function has_enter(e, callback){if ( e.keyCode == 13 ){ eval(callback); }}

function qlogin() {
	var user = $('#qusername').val();
	var pass = $('#quser_password').val();

	login(user, pass);
}

function login (user, pass) {
	$.post( url+'customers/login', {
		username:  user,
		user_password:pass
	},function(data){
		if(data == 1) { window.location = url+'profiles/home/'; }
		else{  if( $('#form_errors').size() > 0 ){$('#form_errors').html(data).css({ color: 'red' });}else{ alert(data); } }
	});	
}

function in_array (needle, haystack, argStrict) 
{
    var key = '', strict = !!argStrict;

    if (strict) 
    {
        for (key in haystack) 
        {
            if (haystack[key] === needle) 
            {
                return true;
            }
        }
    } else 
    {
        for (key in haystack) 
        {
            if (haystack[key] == needle) 
            {
                return true;
            }
        }
    }
 
    return false;
}

function str_replace (search, replace, subject, count) 
{
    var i = 0, j = 0, 
    temp = '', repl = '', 
    sl = 0, fl = 0, 
    f = [].concat(search), 
    r = [].concat(replace), 
    s = subject,
    ra = r instanceof Array, 
    sa = s instanceof Array;
    s = [].concat(s);

    if (count) { this.window[count] = 0; }

    for (i=0, sl=s.length; i < sl; i++) 
    {
        if (s[i] === '') continue;

        for (j=0, fl=f.length; j < fl; j++) 
        {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) 
            {
                this.window[count] += (temp.length-s[i].length)/f[j].length;
            }
        }
    }

    return sa ? s : s[0];
}

function go_to_page (obj) {
	if (obj instanceof Object && obj.val()) {
		var page = parseInt(obj.val());
		var src = $('#pagination_template').val();
		if ( !isNaN(page) && page > 0 && src ) {
			window.location = str_replace( "{page}", obj.val(), src );
		}
	}
}

function forgotten_password() {
	var secure_code = $('#code');
	var m = $('#pass_rec_mail');
	var u = $('#pass_rec_username');//$('#zoom :text[name="username"]');
	$.ajax({ 'url':url+'ajax/forgotten_password', data:{ username: u.val(), mail: m.val(), code:secure_code.val() }, dataType:"script", type:"POST"});
}

function set_skin(skin_id) {
	$.getScript(url+'/home/set_skin/'+skin_id, function(r){ });
}

/** widgets **/
var extWidget = {
	preview_panel: function (elem, name) {
		if ( !elem ){$('#preview_'+name).hide(); return;}
		var offset = $(elem.parentNode).offset();
		var img = elem.getElementsByTagName('img')[0];
		var descr = img.getAttribute('alt').replace(/,/g, '<br />');
		var html = '<img src="'+img.getAttribute('src').replace(/80x80/, '120x120')+'" /><br /><strong>'+descr+'</strong><br />';
		$('#preview_'+name).html(html).css({left:(offset.left-20)+'px',top:(offset.top-20)+'px', display:'block', width:'auto', height:'auto'}).mouseout(function(){extWidget.preview_panel(false, name);}).click(function(){ window.location = elem.getAttribute('href') });
	}
};

