// JavaScript Document

$(document).ready(function(){
						   
	var bIE6 = (document.all && !window.opera && !window.XMLHttpRequest);

	$('a[href*="#NewsletterBox"]').click(function() {
		if ( $(this).hasClass('active') ) {
			$('#Footer .col-2-1 .slider').animate({height:"39px", top:"60px"}, function(){
				$('a[href*="#NewsletterBox"]').removeClass('active');
			});
		} else {
			$('a[href*="#NewsletterBox"]').addClass('active');
			if (!bIE6) {
				$('#Footer .col-2-1 .slider').animate({height:"208px", top:"-109px"});
			}
			else {
				$('#Footer .col-2-1 .slider').animate({height:"211px", top:"-112px"});
			}
		}
		return false;
	});
	
	$('#startMovie').flash(
		{
			src: 'http://de.sevenload.com/pl/c8NBnfX/930x523/swf',
			width: 930,
			height: 523,
			
			allowFullscreen: 'true',
			allowScriptAccess: 'always',
			movie: 'http://de.sevenload.com/pl/c8NBnfX/930x523/swf'
		},
		{
		}
	);
	
	var oRecommendForm = $('#RecommendForm');
	
	oRecommendForm.submit(function()
	{
        oRecommendForm.find('input[type*="image"]').hide();
        oRecommendForm.find('img').show();
	});

    $('a[rel*="fb-share-popup"]').click(function()
    {
        return openSharePopup($(this).attr('href'), 600, 300);
    }) 

    $('a[rel*="tw-share-popup"]').click(function()
    {
        return openSharePopup($(this).attr('href'), 800, 500);
    }) 

	
	loadPetitionPeople();
    
    $('#CountrySelector').change(function()
    {
        if ( $(this).find('option:selected').val() == 'Deutschland' ) {
            toggleTableRow('.state-tr', 1);
        } else {
            toggleTableRow('.state-tr', 0);
        }
    });
});

var openSharePopup = function(_sHref, _iWidth, _iHeight)
{
    //var _iWidth = 600;
    //var _iHeight = 300;
    var _iWinL = (screen.width - _iWidth)/2;
    var _iWinT = (screen.height - _iHeight)/2;
    if (_iWinL < 0) _iWinL = 0;
    if (_iWinT < 0) _iWinT = 0;
    var _sSettings = 'height=' + _iHeight + ',';
    _sSettings += 'width=' + _iWidth + ',';
    _sSettings += 'top=' + _iWinT + ',';
    _sSettings += 'left=' + _iWinL + ',';
    _sSettings += 'resizeable=0,scrolling=0';    
    
    var _oWin = window.open(_sHref, '_facebook_share', _sSettings);
    _oWin.focus();
    
    return false;
};

var toggleTableRow = function(_sElemToFetch, _bShow)
{
    if (!_sElemToFetch) {
        return;
    }
    
    var _oTableRow = $(_sElemToFetch);
    
    if (_bShow && !_oTableRow.hasClass('show-tr')) {
        _oTableRow.removeClass('hide-tr').addClass('show-tr');
    } else if (!_bShow && !_oTableRow.hasClass('hide-tr')) {
        _oTableRow.removeClass('show-tr').addClass('hide-tr');
    }
};

var loadPetitionPeople = function()
{
    var _sOutput = '';
    
    $.post('/petition/ajax-fetch-latest-signees', {}, function(_oData)
    {
        $(_oData).each(function (iIndex, oValue) {
            _sOutput += '<li>';
            _sOutput += '<span class="pers-count">' + oValue['position'] + '</span>';
            _sOutput += '<span class="pers-info">';
            _sOutput += '<strong>' + oValue['pet_name'] + ',</strong> ' + oValue['formatted_country'] + (oValue['formatted_state'] ? ', ' + oValue['formatted_state'] : '');
            if (oValue['formatted_comment'] != '') {
                var _sMoreLink = '';
                
                if (oValue['formatted_comment'].match(/(\.\.\.)/)) {
                    _sMoreLink = ' <a class="show-full-comment" longdesc="' + oValue['pet_comment'] + '">mehr</a>';
                }
                 
                _sOutput += '<em><strong>&raquo;</strong> ' + oValue['formatted_comment'] + ' <strong>&laquo;</strong>' + _sMoreLink + '</em>';

            }
            _sOutput += '</span>';
            _sOutput += '<span class="date-info">' + oValue['formatted_since'] + '</span>';
            _sOutput += '</li>';
        });
        
        if (_sOutput) {
            $('#PetitionPeopleList').html(_sOutput);

            $("#PetitionPeopleList a.show-full-comment").click(function() { return false; });
            $("#PetitionPeopleList a.show-full-comment").tooltip(
            { 
                bodyHandler: function()
                { 
                    var _sReturn = '<span class="tooltip-content">' + $(this).attr('longdesc') + '</span><span class="tooltip-bottom"></span>';
                    return _sReturn;
                },
                left : -175,
                showURL: false 
            });
        }
    }, 'json');
    
    $.post('/petition/ajax-petition-counter-box',function(_sData)
    {
        $('.petition-counter-box').html(_sData);
        
        var _iLeftPos = $('#PetitionCounter span:last').position();
        $('#PetitionCounter div').css({ left : _iLeftPos.left - 92 }); 
    });
    
    
    window.setTimeout(loadPetitionPeople,60000);
};

var currentFormElem = null;
var showHideValue = function(obj,val)
{
	if (obj != currentFormElem)
	{
		currentFormElem = obj;
		if (obj.value == val)
			obj.value = '';
			
		obj.onblur = function()
		{
			showHideValue(obj,val);
		}
	}
	else
	{
		if (obj.value == '')
			obj.value = val;
		
		obj.onblur = null;
		
		currentFormElem = null;
	}
};
