/**
 * @author nuey, ferenc
 */

$(document).ready(function(){

    $('#blue_harvest').insertAfter('#footer');
    $('<div id="bh_window_close">Close</div>').appendTo('#bh_header');
    $('#taf_link, #sb_link').click(openTaf);
       
    $('#taf_tab > a').click(function(){
        $(this).parent('li').addClass('active');
        $('#bh_footer p').show();
        $("#sb_tab").removeClass('active');
        
        //add active state for ie6
        if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == '6') {
            $(this).parent('li').css('background-position', '0 -21px');
            $("#sb_tab").css('background-position', '0 0');
        }
        return false;
       
    });
    $("#sb_tab > a").click(function(){
        $(this).parent('li').addClass('active');
        $('#bh_footer p').hide();        
        $("#taf_tab").removeClass('active');
        
        //add active state for ie6
        if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == '6') {
            $(this).parent('li').css('background-position', '0 -21px');
            $('#taf_tab').css('background-position', '0 0');
        }
        return false;
    });
    $('#bh_message > a').click(function(){
        $(this).parent('li').addClass('active');
        return false;
    });
    
    $('#bh_window_close').hover(function(){
        $(this).css('cursor', 'pointer');
    }, function(){
        $(this).css('cursor', 'default');
    });
    
    if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == '6') { //IE 6

        // IE6 does not support the '>' CSS operator, so we add classes to the tab elements
        $('#bh_tabs > li').addClass('ie6tab');
        $('#bh_tabs > li > a').addClass('ie6tablink');
    }

    // Terms and Conditions popup for Tell a friend.
	$('a#taf_tac').click(function(){
        window.open('/files/taf/terms_and_conditions.html','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=640,left=0,top=0');
        return false;
    });

});

var ffWrapper = false;	

function openTaf(){
    $('#bh_footer p').show();
    $('#bh_tabs li').removeClass('active'); // removes initial leftover actives
    $('embed, object').css('visibility','hidden'); // hides Flash
    triggerSBlinks(); // Adds the social bookmarks actions

    if ($(this).is('#taf_link')) {        
        $('#taf_tab').addClass('active');
    } else {
        $('#sb_tab').addClass('active');
        $('#bh_footer p').hide();
    }

    $('<div id="bh_window_hotspot"></div>').insertAfter('#footer').css('display', 'block').fadeTo(400, 0.7);
    
    if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == '6' || 
		(
			jQuery.browser.mozilla && jQuery.browser.version.substring(0,3) == '1.8' && navigator.appVersion.indexOf("Mac")> -1 
		)
    ) { //IE 6 && ff2 mac

    	//set window on center of screen 
    	(function positionBlackWindow(){
            
            var margin = ($(window).height() - $('#blue_harvest').height()) / 2;
      
            $('#blue_harvest').animate({
                top: margin + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)
            });
            
            $('#blue_harvest').css('margin-top', 0);

            //keep position on scrolling
            jQuery(window).scroll(positionBlackWindow);
        })();
    	
    	$('#blue_harvest').css({'position': 'absolute', 'display': 'block'}).fadeTo(400, 1);
        
    } else {

    	$('#blue_harvest').show().fadeTo(400, 1);

    }

    $('#bh_window_hotspot, #bh_window_close').click(closeTaf);
    
    return false;
}

function closeTaf(){ // Closes the Tell A Friend window and fades out the overlay.
    $('#bh_window_hotspot').fadeTo(400, 0).remove();
    $('#blue_harvest').fadeTo(100, 0).hide();
    $('embed, object').css('visibility','visible');
    jQuery(window).unbind('scroll');
    return false;
}

function triggerSBlinks(){ // Triggers the opening of sized pop-up window for social bokmarks.
    $('#sb_tab > #sb_container > ul > li > a').each( function(i) {
        $(this).click( function() {
            window.open($(this).attr('href').split('#')[0], $(this).parent().attr("id"), $(this).attr('href').split('#')[1]);
            return false;
        });
    });
    $('#facebook a').click(function(){ // Adding facebook stumbleupon pop-up window
        window.open('http://www.stumbleupon.com/submit?url=' + encodeURIComponent(location.href) +
                    '%2Farticle.php%26title%3D' + encodeURIComponent(document.title), 'delicious', 'toolbar=no,width=700,height=400');
        return false;
    });
    return false;
}

// ________________________________________________________________________________________________
//                                                                    A J A X   V A L I D A T I O N
jQuery.fn.clearForm = function(){
    return this.each(function(){
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form') {
            return $(':input', this).clearForm();
        }
        if (type == 'text' || type == 'password' || tag == 'textarea') this.value = '';
        else if (type == 'checkbox' || type == 'radio') this.checked = false;
        else if (tag == 'select') this.selectedIndex = -1;
    });
};

function validate_email(email){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}

function validate_form(){
    var error = false;
    if (!DWRUtil.getValue("email_address").length ||
    !validate_email(DWRUtil.getValue("email_address"))) {
        $('#label_email_address').css('color', 'red');
        error = true;
    } else {
        $('#label_email_address').css('color', 'black');
    }
    if (!DWRUtil.getValue("your_email_address").length ||
    !validate_email(DWRUtil.getValue("your_email_address"))) {
        $('#label_your_email_address').css('color', 'red');
        error = true;
    } else {
        $('#label_your_email_address').css('color', 'black');
    }
    if (!DWRUtil.getValue("confirmation").length) {
        $('#label_confirmation').css('color', 'red');
        error = true;
    } else {
        $('#label_confirmation').css('color', 'black');
    }
    if (!DWRUtil.getValue("your_name").length) {
        $('#label_your_name').css('color', 'red');
        error = true;
    } else {
        $('#label_your_name').css('color', 'black');
    }
    if (DWRUtil.getValue("personal_message").length > 1000) {
        $('#label_personal_message').css('color', 'red');
        error = true;
    } else {
        $('#label_personal_message').css('color', 'black');
    }
    if (error) {
        return false;
    }
    var data = [DWRUtil.getValue("email_address"), DWRUtil.getValue("toName"), DWRUtil.getValue("message"), DWRUtil.getValue("your_email_address"), DWRUtil.getValue("confirmation"), DWRUtil.getValue("article_id")];
    CaptchaValidator.validate(data, validateCalllback);
    return false;
}

function reloadImage(imageId){
    // calling and displaying a new security image (captcha) for a given img element id
    document.getElementById(imageId).src = document.getElementById(imageId).src + '#';
}

function validateCalllback(accepted){
    if (!accepted.length) {
        reloadImage('captcha_image');
        $('<div class="dialog_container" id="message_success"><p><strong>Your e-mail was sent successfully</strong></p><a id="taf_resend" href="#"><img src="./files/gfx/taf/taf_button_resend.png" alt="Send to another friend" title="" /></a></div>').appendTo('li#taf_tab.active');
        $('#taf_resend').click(function(){
            $('input#email_address, input#confirmation').attr('value', '')
            $('input#email_address').focus();
            $('input#confirmation').attr('value', '');
            $('#message_success').fadeTo(200, 0).remove();
            return false;
        });
    } else {
        $('#label_confirmation').css('color', 'red');
    }
}
