function mediayearupdate( hash ) {
	var mediahash = hash.replace('#media_', '');
	var currentContent = $('.entryContainer').html();
	$('.entryContainer').html('<div class="loading"></div>');
	$.ajax({
			type: 'POST',
			url:  'index.php?mediacenterupdate=true',
			data: mediahash,
			dataType: 'text',
			success: function(data, status){
				if( status == 'success' || status == 'notmodified' ) {
					$('.entryContainer').html(data);
					window.location.hash = hash;
				}
			},
			error: function() {
				$('.entryContainer').html('Cererea nu a putut fi completata. Va rugam incercati din nou.<br />'+ currentContent);
			}
	});
	return false;
};

/**
 * for cookies
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function() {
	/* DISPLAY FRONT VIDEO */
	$('.videoTitle').text($('.videoLinks a:first').attr('title'));
	$('.videoContainer').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="440" height="270" id="videoberarii" name="videoberarii"><param name="movie" value="http://www.berariiromaniei.ro/ro/player.swf"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="wmode" value="transparent"><param name="flashvars" value="file='+$('.videoLinks a:first').attr('href')+'&screencolor=000000&image='+ $('.videoLinks a:first').attr( 'rel' ) +'"><embed type="application/x-shockwave-flash" id="berariivideo" name="berariivideo" src="http://www.berariiromaniei.ro/ro/player.swf" width="440" height="270" bgcolor="000000" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file='+$('.videoLinks a:first').attr('href')+'&screencolor=000000&image='+ $('.videoLinks a:first').attr( 'rel' ) +'" /></object>');
	
	/* CHANGE FRONT VIDEO */
	$('.videoLinks a').click(function() {
		$('.videoLinks a').removeClass('current');
		$(this).addClass('current');
		$('.videoTitle').text($(this).attr('title'));
		$('.videoContainer').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="440" height="270" id="videoberarii" name="videoberarii"><param name="movie" value="http://www.berariiromaniei.ro/ro/player.swf"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="wmode" value="transparent"><param name="flashvars" value="file='+$(this).attr('href')+'&screencolor=000000&image='+ $(this).attr( 'rel' ) +'"><embed type="application/x-shockwave-flash" id="berariivideo" name="berariivideo" src="http://www.berariiromaniei.ro/ro/player.swf" width="440" height="270" bgcolor="000000" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file='+$(this).attr('href')+'&screencolor=000000&image='+ $(this).attr( 'rel' ) +'" /></object>');
		return false;
	});
	
	$('a[href$=.pdf]').addClass('pdf');
	
	$('a[href$=.doc], a[href$=.docx').addClass('doc');
	
	$('a.imgColorbox').colorbox();
	
	$('a.brosuraColorbox').colorbox({width:"50%", inline:true, href: $(this).attr( 'href' ) });
						  
	$('.footerLinks li:last, ul.mediaCat li:last').css('border', '0');
	
	/* SCROLL ABOUT */
	$('.about-down').click(function() {
		var oContent = $('.aboutContent').offset();
		var oWrap = $('.aboutWrap').offset();
		if( oWrap.top + ( $('.aboutWrap').height() / 2 ) < oContent.top + $('.aboutContent').height() ) $('.aboutContent').animate({marginTop: "-=100"},400);
		return false;
	});
	
	$('.about-up').click(function() {
		var oContent = $('.aboutContent').offset();
		var oWrap = $('.aboutWrap').offset();
		if( oWrap.top != oContent.top && oWrap.top > oContent.top ) $('.aboutContent').animate({marginTop: "+=100"},400);
		return false;
	});
	
	/* SCROLL CAMPAIGN */	
	$('.campaign-more').click(function() {
		var inaltime = $(this).parent().children('.campaignContent').css('height');
		if( inaltime == 'auto' ) {
			$(this).parent().children('.campaignContent').css('height', '200px');
			$(this).html('<img src="http://www.quambu.com/berarii/wp-content/themes/berarii2.0/images/arrow_down.png" alt="" width="24" height="24" />');
		} else {
			$(this).parent().children('.campaignContent').css('height', 'auto');
			$(this).html('<img src="http://www.quambu.com/berarii/wp-content/themes/berarii2.0/images/arrow_up.png" alt="" width="24" height="24" />');
		}
		return false;
	});
	
	/* CHANGE TABS */
	$('.tabItem').css('display', 'none');
	if( !$('.tabItem:first').hasClass( 'noShow') )
		$('.tabItem:first').css('display', 'block');
	$('.tabs li a').click(function() {
		$('.tabs li, .cTabs li').removeClass('current');
		$('.tabItem').hide();
		$($(this).attr('href')).show();
		$(this).parent().addClass('current');
		return false;
	});	
	$('.cTabs li a').click(function() {
		$('.tabs li, .cTabs li').removeClass('current');
		$('.tabItem').hide();
		$($(this).attr('href')).show();
		$(this).parent().addClass('current');
		window.location.hash = 'member_'+ $(this).attr('href').replace('#member-', '');
		return false;
	});
	
	/* MODAL */
	$('.modalYes').click(function() {
		var dayTest = /^((0?[1-9])|([1-2][0-9])|(3[0-1]))$/;
        var monthTest = /^((0?[1-9])|(1[0-2]))$/;
        var yearTest = /^(19([1-9][0-9])|(20[0-1][0-2]))/;
        var errors = false;
        if (!dayTest.test($('#brDay').val())) {
        	errors = true;
        }
        if (!monthTest.test($('#brMonth').val())) {
        	errors = true;
        }
        if (!yearTest.test($('#brYear').val())) {
        	errors = true;
        }

        var age = new Date((parseInt($('#brYear').val()) + 18), $('#brMonth').val(), $('#brDay').val());
        var today = new Date();
        if((today.getTime() - age.getTime()) < 0) {
            errors = true;
        }

        if (errors) {
        	$('.errors').show();
        } else {
        	$('.errors').hide();
        	createCookie('modalBerarii', 'showmodal');
        	//$.cookie('modalBerarii', 'showmodal');
			$.unblockUI();
        }
	});
	
	$('.modalNo').click(function() {
		window.location.href = 'http://www.alcoolulnutefacemare.ro';
	});
	
	/* LOAD CONTENT BASED ON HASH */
	var hash = document.location.hash;
	if( hash.search('member_') > 0 ) {
		$('.cTabs li').removeClass('current');
		hash = hash.replace('#member_','');
		$('a[href=#member-'+hash+']').parent().addClass('current');
		$('.tabItem').hide();
		$('#member-'+hash).show();
	} else if( hash.search('media_') > 0 ) {
		var mediahash = hash.replace('#media_', '');
		var currentContent = $('.entryContainer').html();
		tophash = mediahash.split('&');
		toplink = '#media_'+ tophash[0];
		$('ul.mediaCat li').removeClass('current');
		$('a[href='+toplink+']').parent().addClass('current');
		$('.entryContainer').html('<div class="loading"></div>').show();
		$.ajax({
				type: 'POST',
				url:  'index.php?mediacenterupdate=true',
				data: mediahash,
				dataType: 'text',
				success: function(data, status){
					if( status == 'success' || status == 'notmodified' ) {
						$('.entryContainer').html(data);
					}
				},
				error: function() {
					$('.entryContainer').html('Cererea nu a putut fi completata. Va rugam incercati din nou.<br />'+ currentContent);
				}
		});
	} else {
		$('ul.mediaCat li:first').addClass('current');
		$('.entryContainer').show();
	}
	
	/* MEDIA CENTER UPDATE */
	$('ul.mediaCat li a').click(function() {
		var mediahash = $(this).attr('href').replace('#media_', '');
		var currentContent = $('.entryContainer').html();
		var t = $(this);
		$('.entryContainer').html('<div class="loading"></div>');
		$.ajax({
				type: 'POST',
				url:  'index.php?mediacenterupdate=true',
				data: mediahash,
				dataType: 'text',
				success: function(data, status){
					if( status == 'success' || status == 'notmodified' ) {
						$('.entryContainer').html(data);
						$('ul.mediaCat li').removeClass('current');
						t.parent().addClass('current');
						window.location.hash = t.attr('href');
					}
				},
				error: function() {
					$('.entryContainer').html('Cererea nu a putut fi completata. Va rugam incercati din nou.<br />'+ currentContent);
				}
		});
		return false;
	});
});

$(window).load(function() {
	$('#animation').nivoSlider({
		effect:'random',
		slices:20,
		animSpeed:500,
		pauseTime:10000,
		startSlide:0,
		directionNav:false,
		pauseOnHover:false
	});
	
	var modal = $.cookie('modalBerarii');
	if( modal != 'showmodal' ) {
		$.blockUI({
			css: {
				background: '#feed7c',
				border: '0',
				color: '#1c2933',
				cursor: 'normal',
				width: '28%',
				padding: '0.5% 1%',
				top: '25%'
			},
			overlayCSS: {
				opacity: 1,
				cursor: 'normal'
			},
			message: $('#modalBerarii'),
			centerX: 'true',
			centerX: true,
			centerY: true,
		});
	}
});
