$(function(){ 
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();
});


//Gallery

function galleryNext(event)
	{

	var res=$(".gallerybig").attr("id");
    var article = /gallery([0-9]*)/i.exec( res );

	var res=$("#glarge img").attr("id");
	var t = /image([0-9]*)/i.exec( res );

	var res=$("#glarge img").attr("name");
	var allpic = /image([0-9]*)/i.exec( res );

    var offset=parseInt(t[1])+1;
        
	if(offset>0)
		{$(".ppic").show();
		}
	if(offset==(allpic[1]-1))	$(".npic").hide();

	$("#glarge img").attr("id","image"+offset);
	$.get("/scripts/_script_gallery_get.php", {"aID":article,"offset":offset} , galleryResult );
	}
	
	function galleryPrev(event)
	{
        var res=$(".gallerybig").attr("id");
        var article = /gallery([0-9]*)/i.exec( res );

	var res=$("#glarge img").attr("id");
    var t = /image([0-9]*)/i.exec( res );
	
	var res=$("#glarge img").attr("name");
	var allpic = /image([0-9]*)/i.exec( res );
	
	var offset=parseInt(t[1])-1;
	if(offset<(allpic[1]-1)) $(".npic").show();
        if(offset<1)
		{
		$(".ppic").hide();
		$(".npic").show();
		}
	$("#glarge img").attr("id","image"+offset);
	$.get("/scripts/_script_gallery_get.php", {"aID":article,"offset":offset} , galleryResult );
	}
	
	 function galleryResult(data)
   {

        var res=data.split("|");
        var r=parseInt(res[1]);
        var h=res[2];
        var url=res[3];
        var alt=res[4];
        
	if(r)
	{
    $("#glarge img").hide().attr({"src": "/"+url+"?"+new Date().getTime()})
	$("#glarge").attr("style","height:"+h+"px;");
	
	
	$("#glarge>img").load(function(){$("#glarge>img:hidden").fadeIn("slow")});
	$("#glarge>img").attr("alt",alt);
	}
	else
	{
	$(".npic").hide();
        var res=$("#glarge img").attr("id");
        var t = /image([0-9]*)/i.exec( res );

	var offset=parseInt(t[1])-1;
        if(offset<1) $(".ppic").hide();
	$("#glarge img").attr("id","image"+offset);
	}
   }


//End Gallery
 function validate_email(value) {
			// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
			if( /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value)) return 1;
			else return 0;
		}
function strstr( haystack, needle, bool ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
 
    var pos = 0;
 
    pos = haystack.indexOf( needle );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function countLink(wordID,fatherID,target)
{

	if(target=="_self")
		this.document.location.href="/countwords/"+wordID+"/"+fatherID+"/";
	else 
		window.open("/countwords/"+wordID+"/"+fatherID+"/");
}


$(document).ready(function(){

	/*=================reklamni vryzki================*/
        if($("#articleID").attr("id"))  
        {				
		
			var d = new Date();
			$.post("/getwords.html?"+d.getTime()+"",{},function(data)
			{
				var res=data.split("|");			   
				if(res[0]==1)  
				{					
					$("#articleID").load("/words.html"+d.getTime()+"");						
					
				}
			}     
				);
		}		
	/*==================================================*/	
						   
	$("#bliasak_awards").css("color","red");					   
						   
    $(".heading-info a.send, #sendnow a.totop, a.send").click(function () {
		  $("#sendnow").toggle();
	});	

	$("#sendtofriend").submit(function(){
   // var page=strstr(location.href,"/p3");
   var page=location.href;
	if($("#yourname").val()&&$("#recname").val()&&$("#yourmail").val()&&$("#recmail").val()&&validate_email($("#yourmail").val())&&validate_email($("#recmail").val())&&page)    
	 {
	     
    $.post("/scripts/_script_sendarticle.php",{"name1":$("#yourname").val(),"name2":$("#recname").val(),"email1":$("#yourmail").val(),"email2":$("#recmail").val(),"page":page},function(data) { 
	    var res=data.split("|");
	    if(res[1])
	           {
                alert(res[0]);
	            $("#yourname").val('');
	            $("#recname").val('');
	            $("#yourmail").val('');
	            $("#recmail").val('');
	            $("#sendnow").hide();
                }
        else alert(res[0]);        
	    });
        }
       else alert("Моля попълнете полетата"); 
	return false;
	});
	
	//------komentari
	    if($("#comments_block").attr("id"))
	    {
            var linka=location.href;
            var linka_arr=linka.split("p1_"); 
            if(linka_arr.length==2)
            {             
               level=1; 
            }
            else
                { linka_arr=linka.split("p2_");
                   if(linka_arr.length==2) level=2;
                }
         
           if(level)
            {var id=linka_arr[1].split("_");
              id=parseInt(id[0]);     
                
                   if(id>0&&!isNaN(id*1))
                    {
                       $.post("/scripts/_script_comments.php",{"father":id,"level":level},function(data){
                           var res=data.split("|");
                           if(res[1])
                                { 
                                    $("#comments_block div dl").html(res[3]);
                                    $("#comments_block h2").html(res[2]);
                                }
                           });
                    }  
            }
        }   
	//------End of komentari
	
	
	
	if($("#gallery_for_star").attr("id"))
	    {
	        $("#gallery_for_star ul  li").click(function(){
	        var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                var old_str=$("#gallery_for_star ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                if(txt=="видео") 
                    {$("#gallery_for_star").addClass("pink");
                      $("#the_star_galery").hide();
                      $("#the_star_galery").removeClass("sel");
                      $("#the_star_video").show();
                      $("#the_star_video").addClass("sel");
                      var temp=$("#gallery_for_star .boxmore a").eq(1).attr("href");
                      $("#gallery_for_star .boxmore a").eq(1).attr("href",temp.replace(/photo/,"video"));
                    }
                else {
                    $("#gallery_for_star").removeClass("pink");
                    $("#the_star_galery").show();
                    $("#the_star_galery").addClass("sel");
                    $("#the_star_video").hide();
                    $("#the_star_video").removeClass("sel");   
                     var temp=$("#gallery_for_star .boxmore a").eq(1).attr("href");
                      $("#gallery_for_star .boxmore a").eq(1).attr("href",temp.replace(/video/,"photo"));
                    }
              }
                 
            });
            
             offset=452;
             $("#agallery a.prev").click(function(){
                   if($("#the_star_galery").attr("class")=="sel") id="gallery_for_star_photo";
                    else id="gallery_for_star_video";
                    
                     
                   var pos=parseInt($("#"+id).css("left"));
                   pos=pos+offset;
                   if(pos<=10) $("#"+id).animate({ left: pos},  "fast");
                    });
                    
             $("#agallery a.next").click(function(){
                     if($("#the_star_galery").attr("class")=="sel") id="gallery_for_star_photo";
                    else id="gallery_for_star_video";
                 
                    var pos=parseInt($("#"+id).css("left"));
                    pos=pos-offset;
                    var all=parseInt($("#"+id).css("width"));
                    all=parseInt(all-offset);
                    if((-1)*pos<=all) $("#"+id).animate({ left: pos },  "fast");
                    });
     
        }
  var animation_speed=1000;
   if($("#photo_block").attr("id"))  $("#photo_block_container").load("/scripts/_script_gallery.php",{"block":1,"pics":110});
    $("#photo_block ul  li").click(function(){
            var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                $("#photo_block_container").html("<p align=\"center\"><img src=\"/html/im/load2.gif\" alt=\"\" /></p>");
                var old_str=$("#photo_block ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
            
				$("#photo_block_container").load("/scripts/_script_gallery.php",{"param":txt,"block":1,"pics":110},function(){
                $("#photo_block_container").fadeIn(animation_speed);
                });
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                }
            }); 
          var animation_speed=1000;
   if($("#fanclubs_div1").attr("id"))  $("#fanclubs_div1").load("/scripts/_script_fanclub_new.php",{"block":1});
    $("#fanclubs_block1 ul  li").click(function(){
            var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                $("#fanclubs_div1").html("<p align=\"center\"><img src=\"/html/im/load2.gif\" alt=\"\" /></p>");
                var old_str=$("#fanclubs_block1 ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
            
				$("#fanclubs_div1").load("/scripts/_script_fanclub_new.php",{"toptop":txt,"block":1},function(){
                $("#fanclubs_div1").fadeIn(animation_speed);
                });
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                }
            });      
            
            
            
         if($("#fanclubs_div2").attr("id"))  $("#fanclubs_div2").load("/scripts/_script_fanclub_new.php",{"block":2});
    $("#fanclubs_block2 ul li").click(function(){
            var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                $("#fanclubs_div2").html("<p align=\"center\"><img src=\"/html/im/load2.gif\" alt=\"\" /></p>");
                var old_str=$("#fanclubs_block2 ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
            
				$("#fanclubs_div2").load("/scripts/_script_fanclub_new.php",{"toptop":txt,"block":2},function(){
                $("#fanclubs_div2").fadeIn(animation_speed);
                });
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                }
            }); 
            
    var animation_speed=1000;
    $("#toptop_div").load("/scripts/_script_toptop.php",{"design":1});
    
    $("#statii_top ul > li").click(function(){
            var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                $("#toptop_div").html("<p align=\"center\"><img src=\"/html/im/load2.gif\" alt=\"\" /></p>");
                var old_str=$("#statii_top ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
            
				$("#toptop_div").load("/scripts/_script_toptop.php",{"toptop":txt,"design":1},function(){
                $("#toptop_div").fadeIn(animation_speed);
                });
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                }
            });    
    if($("#reporter_block").attr("id"))  $("#reporter_block_container").load("/scripts/_script_gallery.php",{"block":3,"pics":110});
    $("#reporter_block ul  li").click(function(){
            var txt=$(this).text();
            var ch=$(this).find('a');
            if(ch.text())
             {
                $("#reporter_block_container").html("<p align=\"center\"><img src=\"/html/im/load2.gif\" alt=\"\" /></p>");
                var old_str=$("#reporter_block ul > li").find('em');
                var old_txt=old_str.text();
                var old_par=old_str.parent();
            
				$("#reporter_block_container").load("/scripts/_script_gallery.php",{"param":txt,"block":3,"pics":110},function(){
                $("#reporter_block_container").fadeIn(animation_speed);
                });
                old_par.empty();
                old_par.append("<a href=\"javascript:;\">"+old_txt+"</a>");
                $(this).empty();
                $(this).append("<em>"+txt+"</em>");
                }
            });        
       $( ".gallerybig" ).each( function()
       {
           $(this).find( "a.ppic" ).click( galleryPrev );
	       $(this).find( "a.npic" ).click( galleryNext );
       });
           
});




//Article functions
function UserVote(vote,artid)
{
$.post("../../scripts/_script_vote.php",{vote:vote,artid:artid},function(data) {
    var res=data.split("|");
    if(res[1]>0){
    var vote=res[1]*11;
    $(".star-rating li:first").attr("style","width:"+vote+"px;");
    }
    alert(res[0]);
     });
}

	
	
 function changeFont(inc,id){
   var p=document.getElementById(id);
     if(p.style.fontSize){
      var size=parseFloat(p.style.fontSize.replace("em",""));
     }else{
      var size=1.1;  //tuk slagame default stoinosta da se vidi ot css faila :)
     }
    if(!inc)  size=1.1;
    if((size+inc)<= 2.1 && (size+inc)>=0.3)
      p.style.fontSize=size+inc + 'em';
    }
    
function makefavorite(id)
{

$.post("/scripts/_script_makefav.php",{"article":id},function(data){
    alert(data);
    $("a.fav").after('<strong class="fav" title="Любима статия"><em>В любими</em></strong>');
    $("a.fav").remove();
    });

}
