jQuery(document).ready(function(){
    //to animate the latest news bar in header
    jQuery('#latest_news').ticker();
    if(typeof head_slide_option !='undefined'){
        jQuery('#featured img').show();
        // head slider
        jQuery('#head_slide_show').accessNews(head_slide_option);
    
        //middle slide show
        jQuery('#featured').tabs({
            fx:{
                opacity:'toggle'
            }
        }).tabs('rotate', 5000, true).find('.info:first').slideDown();
    }
    if(typeof image_slide_option !='undefined'){
           jQuery('#image_slide_show').accessNews(image_slide_option);
    }

    if(typeof video_slide_option !='undefined'){
           jQuery('#video_slide_show').accessNews(video_slide_option);
    }

/*########################################################################################*/
//show hide the languages in case of more
jQuery('#language_more table').bind('mouseover',function(){
    jQuery('#language_more ul').show();
}).bind('mouseout',function(){
    jQuery('#language_more ul').hide();
});

/*########################################################################################*/
jQuery('#news_counts').tabber();
/*########################################################################################*/
jQuery('#story_counts').tabber();
/*########################################################################################*/
jQuery('#simple_search').tabber();
/*########################################################################################*/
//jQuery('#common_search').tabber();
/*########################################################################################*/
//to tooltip the countries
jQuery('.countries ul li a[title!=\'\'],.block_1 p[title!=\'\'],.gallery_image td a[title!=\'\']').bt({
    positions:['most'],
    fill:'#F6F6BE',
    width:'250px',
    centerPointX:0.3,
    hoverIntentOpts:{
        interval: 100,
        timeout: 100
    }
});
   
//scroll to top
//#########################################################
jQuery('#top').bind('click',function(){
    jQuery("html").animate({
        scrollTop:0
    },{
        duration:1000
    });
})
jQuery('#comment').bind('click',function(){
    jQuery("html").animate({
        scrollTop:(jQuery("#comment_form").offset().top-40)
    },{
        duration:1000
    });
})


jQuery('#back').bind('click',function(){
    history.back(-1);
})



        //image fading
        //#########################################################
        jQuery('.gallery_image td a').bind('mouseover',function(){
            jQuery(this).animate({opacity: "0.5"},100);
        }).bind('mouseout',function(){
            jQuery(this).animate({opacity: "1"},200);
        })

news_break();

});

jQuery(window).load(function(){
    jQuery("#theme").css({"visibility":"visible"});
});


var news_break_timer=setTimeout(news_break,15000);
function news_break(){
    current=calculate_time_zone();
    jQuery.ajax({
        type: "POST",
        url: "_request.php",
        data:"action=news_break&time_current="+current['time_current'],
        beforeSend:function(){
            
            clearTimeout(news_break_timer);
        },
        success: function(result){
            var result=eval('(' + result + ')');
            var reload=false;
            if(result.operation=='success'){
                jQuery('#break_news li').each(function(){
                    if(!in_array(this.id,result.key)){
                        jQuery.ticker.clear(jQuery('#break_news'));
                        jQuery('#'+this.id).remove();
                        reload=true;
                    }
                });
                for(var i=0;i<result.key.length;i++){
                    if(jQuery('#'+result.key[i]).length==0)
                        jQuery('#break_news').append(result.data[i]);
                }

                if(jQuery('#break_news').css('display')=='none' || reload){
                    jQuery('#break_news').slideDown('slow').ticker(7000);
                }
            }
            else{
                if(jQuery('#break_news').css('display')!='none'){
                    jQuery.ticker.clear(jQuery('#break_news').slideUp('slow').empty());
                }
            }
            news_break_timer=setTimeout(news_break,15000);
        }
    });
}

function in_array(needle,array){
    var bool=false;
    for(var i=0;i<array.length;i++){
        if(needle==array[i]){
            bool=true;
            break;
        }
    }
    return bool;
}


//########################################################
jQuery('#comment_reload').bind('click',function(){
    jQuery('#comment_form').find('input').attr('disabled','disabled');
    jQuery('#comment_spam_image').hide();
    jQuery('#comment_spam_image').attr('src','_request.php?spam_auth=comment_spam&'+Math.random());
    setTimeout(function(){
        jQuery('#comment_spam_image').slideDown(400);
        jQuery('#comment_form').find('input').attr('disabled','');
    },2000)
});
jQuery('#comment_detail').bind('keyup',function(){
    if(jQuery(this).val().length>600){
        var value=jQuery(this).val().substr(0,600);
        jQuery(this).val(value);
    }
    jQuery('#comment_detail_counter').html(jQuery(this).val().length+'/600');
});

//#################################################################
function mail_list_reg(){
    jQuery.ajax({
        type: "POST",
        url: "_request.php",
        data:"action=mail_register&mail_name="+jQuery('#mail_name').val()+'&mail_email='+jQuery('#mail_email').val(),
        beforeSend:function(){
          jQuery('#mail_loader').show();
          jQuery('#mail_register').find('input').attr('disabled','disabled');
          jQuery('#mail_register_message').slideUp();
        },
        success: function(result){
           var result=eval('(' + result + ')');
           jQuery('#mail_register_message').attr('class',result.operation).html(result.message);
           jQuery('#mail_register_message').slideDown();
           jQuery('#mail_loader').hide();
           jQuery('#mail_register').find('input').attr('disabled','');
           if(result.operation=='success'){
               jQuery('#mail_clear').click();
                setTimeout(function(){
                   jQuery('#mail_register_message').slideUp();
               },4000);
           }
        }
    });
}

jQuery('#mail_clear').bind('click',function(){
    jQuery('#mail_register').find('input[type=text]').val('');
});


function comment_send(key,value,action){
    current=calculate_time_zone();
    jQuery.ajax({
        type: "POST",
        url: "_request.php",
        data:"action="+action+"&"+key+"="+value+'&comment_title='+jQuery('#comment_title').val()+'&comment_name='+jQuery('#comment_name').val()+'&comment_email='+jQuery('#comment_email').val()+'&comment_detail='+jQuery('#comment_detail').val()+'&comment_spam='+jQuery('#comment_spam').val()+"&comment_datetime="+current['time_current']+"&comment_offset="+current['time_offset'],
        beforeSend:function(){
          jQuery('#comment_loader').show();
          jQuery('#comment_form').find('input,textarea').attr('disabled','disabled');
          jQuery('#comment_message').slideUp();
          jQuery('#comment_reload').hide();
          
          
        },
        success: function(result){
            var result=eval('(' + result + ')');
            jQuery('#comment_message').attr('class',result.operation).html(result.message);
            jQuery('#comment_message').slideDown();
            jQuery('#comment_loader').hide();
            jQuery('#comment_form').find('input,textarea').attr('disabled','');
            if(result.operation=='success'){
               jQuery('#comment_clear').click();
               setTimeout(function(){
                   jQuery('#comment_message').slideUp();
               },4000);
            }
            jQuery('#comment_reload').show();
            jQuery('#comment_reload').click();
        }
    });
}
jQuery('#comment_clear').bind('click',function(){
    jQuery('#comment_form').find('input[type=text],textarea').val('');
});
function print_page(key,value,action){
   jQuery('.print_area').printArea({mode:"popup",body_head:jQuery('#logo').html(),body_footer:jQuery('#copyright').html()});
   jQuery.ajax({
        type: "POST",
        url: "_request.php",
        data:"action="+action+"&"+key+"="+value,
        beforeSend:function(){
        },
        success: function(){
        }
    });
    
}

jQuery('#send_to_friend,#send_to_friend_close').bind('click',function(){
    jQuery('#send_to_friend_container').slideToggle();
});


function send_t_friend_mail(key,value,action){
    jQuery.ajax({
        type: "POST",
        url: "_request.php",
        data:"action="+action+"&"+key+"="+value+'&send_to_friend_spam='+jQuery('#send_to_friend_spam').val()+'&from_friend='+jQuery('#from_friend').val()+'&to_friend='+jQuery('#to_friend').val()+'&from_friend_email='+jQuery('#from_friend_email').val()+'&to_friend_email='+jQuery('#to_friend_email').val()+'&title='+document.title,
        beforeSend:function(){
          jQuery('#send_to_friend_loader').show();
          jQuery('#send_to_friend_container').find('input').attr('disabled','disabled');
          jQuery('#send_to_friend_message').slideUp();
          jQuery('#send_to_friend_reload').hide();
        },
        success: function(result){
            var result=eval('(' + result + ')');
            jQuery('#send_to_friend_message').attr('class',result.operation).html(result.message);
            jQuery('#send_to_friend_message').slideDown();
            jQuery('#send_to_friend_loader').hide();
            jQuery('#send_to_friend_container').find('input').attr('disabled','');
            if(result.operation=='success'){
               jQuery('#send_to_friend_clear').click();
               setTimeout(function(){
                   jQuery('#send_to_friend_message').slideUp();
               },4000);
            }
           jQuery('#send_to_friend_reload').show();
           jQuery('#send_to_friend_reload').click();
        }
    });
}

jQuery('#send_to_friend_clear').bind('click',function(){
    jQuery('#send_to_friend_container').find('input[type=text]').val('');
});


jQuery('#send_to_friend_reload').bind('click',function(){
    jQuery('#send_to_friend_container').find('input').attr('disabled','disabled');
    jQuery('#send_to_friend_spam_image').hide();
    jQuery('#send_to_friend_spam_image').attr('src','_request.php?spam_auth=send_to_friend_spam&'+Math.random());
    setTimeout(function(){
        jQuery('#send_to_friend_spam_image').slideDown(400);
        jQuery('#send_to_friend_container').find('input').attr('disabled','');
    },2000)
});



jQuery('#search_clear').bind('click',function(){
    jQuery('#search_title').val('');
    jQuery('#search_detail').val('');
    jQuery('#search_date').date_val('');
    search_country.setComboValue('');
    search_category_organization.setComboValue('');
    search_type.setComboValue('');
    search_type_story_id.setComboValue('');
    search_source.setComboValue('');
    search_writer.setComboValue('');
    jQuery(".story_only,.news_only").css({"display":"none"});
});


function advance_search_send(){
   var query ='_search_result.php?search_title='+jQuery('#search_title').val();
       query+='&search_detail='+jQuery('#search_detail').val();
       query+='&search_date='+jQuery('#search_date').val();
       query+='&search_country='+search_country.getActualValue();
       query+='&search_category_organization='+search_category_organization.getActualValue();
       query+='&search_type='+search_type.getActualValue();
       query+='&search_type_story_id='+search_type_story_id.getActualValue();
       query+='&search_source='+search_source.getActualValue();
       query+='&search_writer='+search_writer.getActualValue();
       jQuery('#search_result').attr('src',query);
       jQuery('#search_loader').css({'visibility':'visible'});
       
}

