/**
 * @author Alexei <alexei@kinecto.ro>
 */
$(function() {
	$('a.collapsilink').each(function() {
		var $this = $(this),
			$panel = $($this.attr('href'));
		$panel.slideUp();
		$this.click(function() {
			$panel.slideToggle();
			return false;
		});
	});

	if ($.blockUI) {
		$(document).bind('contextmenu', function(e) {
			$.blockUI({
				css: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', 'border-radius': '10px', opacity: .5, color: '#fff'},
				overlay: {cursor: 'wait'},
				message: '<span style="font-weight: bold; font-size: 16px; line-height: 24px; color: #fff;">'+ (App.locale == 'ro' ? ' Zamfirescu Racoti Predoiu detine drepturi de proprietate intelectuala cu privire la toate materialele continute in acest Site' : 'Zamfirescu Racoţi Predoiu holds intellectual property rights pertaining to all materials contained by this Site') +'</span>',
				timeout: 3000
			});
			return false;
		});
	}


	/* announce modal dialog */
	/*
	Announce.init();
	Announce.show(
		'<div class="content">\
			<h2>WE\'VE MOVED</h2>\
			<p>We are pleased to announce that effective November 29th, 2010 Zamfirescu Racoti Predoiu relocated to:</p>\
			<p>12, Plantelor St., District 2<br />Bucharest RO – 023974<br />Romania</p>\
			<p>We will continue to use our current telephone and facsimile numbers:</p>\
			<p>Phone: +4021 311 05 17<br />Fax: +4021 311 05 19</p>\
		</div>'
	);
	
       
       Announce.init();
       if(!$.cookie('noulcodcivli')) {
           Announce.show('<a href="#" style="width: 1000px; height: 476px; display: block;"></a>');
       }
       
       $('.announce-close, .o').click(function () {$.cookie('noulcodcivli', 'zrp', { expires: 30 }); window.location = 'http://www.zrp.ro/index.php/ro/cum-va-schimba-noul-cod-civil-regulile-n-afaceri_137'})
            */
        
});

this.Announce = {
	$overlay: $('<div class="announce-overlay" />'),
	$dialog_bo: $('<div class="ot" /><div class="or" /><div class="ob" /><div class="ol" />'),
	$dialog_bi: $('<div class="it" /><div class="ir" /><div class="ib" /><div class="il" />'),
	$dialog_bc: $('<div class="tl" /><div class="tr" /><div class="br" /><div class="bl" />'),
	$dialog_o: $('<div class="o" />'),
	$dialog_i: $('<div class="i" />'),
	$dialog: $('<div class="announce-dialog" />'),
	$close: $('<div class="announce-close" />'),

	init: function() {
		Announce.$overlay.css({opacity: '0.75'});
		$([Announce.$close]).each(function() {
			$(this).click(function() {
				Announce.hide();
				return false;
			});
		});
		Announce.$dialog_o.append(Announce.$dialog_bo).append(Announce.$dialog_bi).append(Announce.$dialog_bc).append(Announce.$dialog_i).append(Announce.$close);
		Announce.$dialog.append(Announce.$dialog_o);
		$('body').append(Announce.$overlay).append(Announce.$dialog);
	},

	show: function(msg) {
		Announce.$overlay.fadeIn(function() {
			Announce.$dialog_i.html(msg);
			Announce.$dialog.fadeIn()
		});
	},

	hide: function() {
		Announce.$dialog.fadeOut(function() {
			Announce.$overlay.fadeOut();
		});
	}
};

$(window).load(function() {
	$('#h .pm')
		.lavaLamp({fx: 'easeOutBack', speed: 700})
		.find('li:has(ul)')
		.mouseover(function() {
			var $me = $(this);
			window.clearTimeout($me.data('delay'));
			$me.find('ul').slideDown('fast').css({'display': 'block'});
		})
		.mouseout(function() {
			var $me = $(this);
			var delayId = window.setTimeout(function() {
				$me.find('ul').slideUp('fast').css({'display': 'none'});
			}, 200);
			$me.data('delay', delayId);
		})
		.find('ul').hide();

	$('.form-search input[type="text"]').each(function() {
		var $that = $(this);
		$that.data('initial-value', $that.val());
		$that
			.focus(function() {
				if (($(this).val() == 'Search') || ($(this).val() == 'Cautare')) {
					$(this).val('');
				}
			})
			.blur(function() {
				if ($(this).val() == '') {
					$(this).val($that.data('initial-value'));
				}
			});
	});
});

var App = {
	locale: 'en',

	init_home: function() {
		var avatar = [$('#box-attorney-1'), $('#box-attorney-2')],
			pics = [
				'home-avatar-anca-danilescu.png',
				'home-avatar-calin-andrei-zamfirescu.png',
				'home-avatar-cosmin-vasile.png',
				'home-avatar-ioana-racoti.png',
				'home-avatar-lucian-danilescu.png',
				'home-avatar-stan-tarnoveanu.png'
			],
			pics_dummy = fisherYatesShuffle(pics),
			img = new Image();
		for (var i in pics) {
			img.src = base_url +'theme/img/'+ pics[i];
		}
		avatar[0].append('<div id="box-attorney-1-dummy"></div>');
		avatar[1].append('<div id="box-attorney-2-dummy"></div>');
		var avatar_dummy = [$('#box-attorney-1-dummy'), $('#box-attorney-2-dummy')];
		avatar[0].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[0] +')');
		avatar_dummy[0].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[0] +')').fadeOut('fast');
		avatar[1].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[1] +')');
		avatar_dummy[1].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[1] +')').fadeOut('fast');
		$('#home-rs-visual').everyTime(5000, 'attorneys', function() {
			pics_dummy = fisherYatesShuffle(pics);

			avatar_dummy[0].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[0] +')').fadeIn('fast', function() {
				avatar[0].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[0] +')');
				$(this).fadeOut('fast');
			});
			avatar_dummy[1].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[1] +')').fadeIn('fast', function() {
				avatar[1].css('backgroundImage', 'url('+ base_url +'theme/img/'+ pics_dummy[1] +')');
				$(this).fadeOut('fast');
			});
		});
	}
};

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [rev. #1]
fisherYatesShuffle = function(o) {
	for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};



/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

