// JavaScript Document

$.fn.cycle.defaults.timeout = 6000;
function animate_poll(){
    $('.survey_image').each(
        function(){
            var o_width=$(this).attr("width");
            $(this).attr("width","0");
            $(this).animate({
                width:o_width+"px"
            },1500);
        });
}
function slide_news_f(){
	/*$(".videos #library li a").wTooltip({
        content: true,        
        callBefore: function(tooltip,node){$(tooltip).html($(node).nextAll("p").text());}
    });*/
    $('.side_news').each(function(ind){
        //alert(ind);
        var p_count=$(".content div",this).length;
        if (p_count==0){
            //alert("لا توجد أخبار");
        }
        else{
            var paging="<div class=\"nav_btns\" id=\"btns_"+ind+"\">";
            for(i=1;i<=p_count;i++){
                paging+="<a href=\"#\">"+i+"</a>";
            }
            paging+="</div>";
            //paging+="<a href=\"#\" class=\"next\" id=\"next_"+ind+"\"></a><a href=\"#\" class=\"prev\" id=\"prev_"+ind+"\"></a>";
            //alert(paging);
            $(".warper",this).after(paging);
        $('.content',this).cycle({
            fx:'scrollLeft',
            speed: 1000,
            timeout:0,
            next:'#next_'+ind,
            prev:'#prev_'+ind,
            pager:  '#btns_'+ind,
            pagerEvent:'click',
            pauseOnPagerHover:true,
            pagerAnchorBuilder: function(idx, slide){
                return '#btns_'+ind+' a:eq(' + (idx) + ') ';
            }
        });
        }
    });
}
$(function() {
    //$('#nav li:eq(0) a').css('margin-top','2px');
    $("#slides_t a p").css("opacity",0.7);
    $('#slides_t').cycle({
        fx:'fade',
        speed: 1000,
        timeout: 4000,
        pager:  '#slide_nav',
        pagerEvent:'mouseover',
        pauseOnPagerHover:true,
        pagerAnchorBuilder: function(idx, slide){
            return '#slide_nav li:eq(' + (idx) + ') ';
        }
    });	
	$('#news_bar .content').cycle({
        fx:'scrollUp',
        speed: 1000,
        timeout: 4000,
        next:'#news_bar .next',
		prev:'#news_bar .prev',		
		pagerEvent:'click',
		easing:'easeOutExpo',
		pauseOnPagerHover:true
    });
    $('#news_ticker').cycle({fx:'slideY',speed: 1000,timeout: 4000, next:'#next', prev:'#prev', pause:1});
    slide_news_f();
    $("#faqs h2").click(function(){
        $("#faqs>div").slideUp();
        $(this).next().slideToggle();
    });

    $('#cite_cont_2 .close').click(function(){
        $("#cite_cont_2").hide("slow");
        return false;
    })
    $("#Map area").click(function(){
        //alert("#city_d_"+($(this).attr("id")));
        $('#cite_cont_2 .content').html($("#city_d_"+($(this).attr("id"))).html());
        var news_p_count=$("#city_d_"+($(this)).attr("id")+" div").length;
        if (news_p_count==0){
            alert("لا توجد أخبار في هذه المحافظة");
        }
        else{
            var paging="";
            for(i=1;i<=news_p_count;i++){
                paging+="<a href=\"#\">"+i+"</a>";
            }
            $('#cite_cont_2 .nav_btns').html(paging);
            $('#cite_cont_2 .title .title').html($(this).attr("title"));
            $("#cite_cont_2").show("slow",function(){
                $('#cite_cont_2 .content').cycle({
                    fx:'scrollLeft',
                    speed: 1000,
                    timeout:0,
                    next:'#cite_cont_2 .next',
                    prev:'#cite_cont_2 .prev',
                    pager:  '#cite_cont_2 .nav_btns',
                    pagerEvent:'click',
                    pauseOnPagerHover:true,
                    pagerAnchorBuilder: function(idx, slide){
                        return '#cite_cont_2 .nav_btns a:eq(' + (idx) + ') ';
                    }
                });
																		   
            });
        }
        return false;
    });
	
    $("#cite_cont_2").css("display","none");
	
    animate_poll();
    var survey;
    $("input[type='submit'][name='poll']").bind('click',survey_click);
    function survey_click(){
        if($(this).val()=="تصويت")
            survey="send_vote";
        else
            survey="show_results";
    }
    /*$("#library li ").hover(function(){
        $("p",this).fadeIn("slow");
    },function(){
        $("p",this).fadeOut("fast");
    });*/
    function submit_function(e){
        var option1;
        var survey_id;
        var this_form=$(this);
        survey_id= $(":input[name='poll_id']",this_form).val();
        option1=$(":radio:checked",this_form).val();
        var sub_data="poll_id="+survey_id+"&check_option="+option1+"&survey="+survey+"&ajax=1";
        var b_height=$(".survey_overlay").parent().height();
        $(".survey_overlay").css('height',b_height);
        $(".survey_overlay").fadeIn(300);
        $.ajax({
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            scriptCharset:"utf-8",
            dataType:"html",
            processData:false,
            cache:false,
            type:"post",
            url: $('.survey').attr("action"),
            data: sub_data,
            success: function(msg){
                if(msg=="please_select"){
                    alert("الرجاء تحديد اختيارك");
                    $(".survey_overlay").fadeOut(800);
                    return false;
                }
                $(this_form).parent().remove();
                $('.polls').append(msg);
                $(".survey_overlay").fadeOut(800);
                animate_poll();
                $('.survey').bind('submit',submit_function);
                $("input[type='submit'][name='survey']").bind('click',survey_click);
            //alert(msg);
            },
            error:function(msg,text){
                alert(text);
            }
        });
        return false;
    }
    function submit_comment(e){
        var option1;
        var survey_id;
        var this_form=$(this);
        var title= $("#title",this_form).val();
        var name= $("#name",this_form).val();
        var email= $("#email",this_form).val();
        var text_= $("#text",this_form).val();
        var subject_id= $("#subject_id",this_form).val();
        
        var sub_data="title="+title+"&name="+name+"&email="+email+"&text="+text_+"&subject_id="+subject_id+"&ajax="+1;
        var b_height=$(".comment_overlay").parent().height();
        $(".comment_overlay").css('height',b_height);
        $(".comment_overlay").fadeIn(300);
        $.ajax({
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            scriptCharset:"utf-8",
            dataType:"html",
            processData:false,
            cache:false,
            type:"post",
            url: $('#comment').attr("action"),
            data: sub_data,
            success: function(msg){
                if(msg!="done"){
                    alert(msg);
                    $(".comment_overlay").fadeOut(800);
                }else{
                    alert("تم إضافة تعليقك بنجاح ، وسيتم نشرة بعد اطلاع إدارة الموقع");
                    $("#title",this_form).val("");
                    $("#name",this_form).val("");
                    $("#email",this_form).val("");
                    $("#text",this_form).val("");
                }
                $(".comment_overlay").fadeOut(800);
                return false;

            },
            error:function(msg,text){
                alert(text);
            }
        });
        return false;
    }
    function submit_reserve(e){
        var this_form=$(this);
        var title= $("#title",this_form).val();
        var name= $("#name",this_form).val();
        var email= $("#email",this_form).val();
        var text_= $("#text",this_form).val();
        var subject_id= $("#subject_id",this_form).val();

        var sub_data="title="+title+"&name="+name+"&email="+email+"&text="+text_+"&subject_id="+subject_id+"&ajax="+1;
        var b_height=$(".comment_overlay").parent().height();
        $(".comment_overlay").css('height',b_height);
        $(".comment_overlay").fadeIn(300);
        $.ajax({
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            scriptCharset:"utf-8",
            dataType:"html",
            processData:false,
            cache:false,
            type:"post",
            url: $('#comment').attr("action"),
            data: sub_data,
            success: function(msg){
                if(msg!="done"){
                    alert(msg);
                    $(".comment_overlay").fadeOut(800);
                }else{
                    alert("تم إضافة تعليقك بنجاح ، وسيتم نشرة بعد اطلاع إدارة الموقع");
                    $("#title",this_form).val("");
                    $("#name",this_form).val("");
                    $("#email",this_form).val("");
                    $("#text",this_form).val("");
                }
                $(".comment_overlay").fadeOut(800);
                return false;

            },
            error:function(msg,text){
                alert(text);
            }
        });
        return false;
    }
    function submit_fatwa(e){
        var option1;
        var survey_id;
        var this_form=$(this);
        var title= $("#title",this_form).val();
        var name= $("#name",this_form).val();
        var email= $("#email",this_form).val();
        var text_= $("#text",this_form).val();

        var sub_data="title="+title+"&name="+name+"&email="+email+"&text="+text_+"&ajax="+1;
        var b_height=$(".comment_overlay").parent().height();
        $(".comment_overlay").css('height',b_height);
        $(".comment_overlay").fadeIn(300);
        $.ajax({
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            scriptCharset:"utf-8",
            dataType:"html",
            processData:false,
            cache:false,
            type:"post",
            url: $('#fatwa').attr("action"),
            data: sub_data,
            success: function(msg){
                if(msg!="done"){
                    alert(msg);
                    $(".comment_overlay").fadeOut(800);
                }else{
                    alert("تم استلام سؤالك وسيتم نشر الإجابة عليه ونشرة");
                    $("#title",this_form).val("");
                    $("#name",this_form).val("");
                    $("#email",this_form).val("");
                    $("#text",this_form).val("");
                }
                $(".comment_overlay").fadeOut(800);
                return false;

            },
            error:function(msg,text){
                alert(text);
            }
        });
        return false;
    }
    function submit_send_subject(e){
        var option1;
        var survey_id;
        var this_form=$(this);
        var f_email= $("#f_email",this_form).val();
        var name= $("#name",this_form).val();
        var email= $("#email",this_form).val();
        var text_= $("#text",this_form).val();
        var subject_id= $("#subject_id",this_form).val();

        var sub_data="f_email="+f_email+"&name="+name+"&email="+email+"&text="+text_+"&ajax="+1+"&subject_id="+subject_id;
        alert(sub_data);
        var b_height=$(".comment_overlay").parent().height();
        $(".comment_overlay").css('height',b_height);
        $(".comment_overlay").fadeIn(300);
        $.ajax({
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            scriptCharset:"utf-8",
            dataType:"html",
            processData:false,
            cache:false,
            type:"post",
            url: $('#send_subject').attr("action"),
            data: sub_data,
            success: function(msg){
                if(msg!="done"){
                    alert(msg);
                    $(".comment_overlay").fadeOut(800);
                }else{
                    alert("لقد تم إرسال الموضوع إلى صديقك بنجاح");
                    $("#f_email",this_form).val("");
                    $("#name",this_form).val("");
                    $("#email",this_form).val("");
                    $("#text",this_form).val("");
                }
                $(".comment_overlay").fadeOut(800);
                return false;

            },
            error:function(msg,text){
                alert(text);
            }
        });
        return false;
    }
    $('.survey').bind('submit',submit_function);
    $('#send_subject').bind('submit',submit_send_subject);
    $('#comment').bind('submit',submit_comment);
    $('#fatwa').bind('submit',submit_fatwa);
    
});
