$().ready(function() {
				   

//jQuery(document).bind('keydown', 'F5',function (evt){ window.location.href=window.location.href; return false; });


//$("a[rel='item_page_gallery']").colorbox({slideshow:true, slideshowStart: 'تشغيل',slideshowStop: 'ايقاف', current:"{current}/{total}"});

	

$("#print_link").click( function(){ window.open($("#print_link").attr("href"),"print_item","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=670, height=530"); return false});

	$('.comment_bit_title').click(function() {
				$('.comment_bit_content').slideUp();
				this_id = $(this).attr('id').split("comment-title-")[1];
				$('#comment-content-' + this_id).slideDown();
	
	}) ;

$('.comment_report').click(function() { $(this).colorbox({width:'40%',height:'250px', href: 'ar/report-comment/' + $(this).attr("id").split('-')[1] + '.html'});});


$('.comment_delete').click(function(){comment_delete($(this)); });
$('.comment_edit').click(function(){comment_edit($(this)); });
$('.comment_ban').click(function(){user_ban($(this)); });

animate_poller();

});


function comment_delete(e)
{
	var id = e.attr("rel");

	if(confirm("delete ?"))
		$.post("be_comment.php",{ "do": "delete", "id": id }, function(data){ alert(data.split("|=|")[0]); if(data.split("|=|")[1] == "1") window.location.reload(); });

}

function comment_edit(e)
{
	var id = e.attr("rel");
	
		$.post("be_comment.php",{ "do": "edit", "id": id }, function(data){
																	 

							if(data.split("|=|")[2] == "1")
							{
								$("#comment-btext-" + id).html('<textarea class="comment_text_input" cols="58" rows="8" name="comment_new_text" id="comment-new-text-' + id + '">' + data.split("|=|")[0] + '</textarea><div align="left"><input type="button" class="button" onclick="comment_edit_save('+id+')" value="save"  /></div>');
								
								$("#comment-btitle-" + id).html('<input name="comment_new_title" id="comment-new-title-' + id + '" size="45" class="comment_title_input" value="' + data.split("|=|")[1] + '" maxlength="100" type="text">');
							}
							
					});

}

function comment_edit_save(id)
{
	var new_txt = $("#comment-new-text-" + id).val();
	var new_title = $("#comment-new-title-" + id).val();
	
	$.post("be_comment.php",{ "do": "update", "id": id, "txt": new_txt, "title": new_title }, function(data){
				
				if(data.split("|=|")[1] == "1")
					{
						$("#comment-btext-" + id).html(new_txt);
						$("#comment-btitle-" + id).html(new_title);
					}
																 
																 
		});
}

function user_ban(e)
{
	var id = e.attr("rel");

	if(confirm("ban this user ?"))
		$.post("be_comment.php",{ "do": "ban", "id": id }, function(data){ alert(data.split("|=|")[0]); if(data.split("|=|")[1] == "1") window.location.reload(); });

}

function animate_poller()
{

poller_option_cc = 1;

$('.poller_bar').each(function(){
							   		
									width_factor = ($(this).parent().width() - 6)/100;
									poller_width = $(this).attr("rel")*width_factor;
									poller_per = $(this).attr("rel");
									//alert($(this).attr("rel"));

							
									$(this).everyTime(10, 'controlled', function() {

										newval = parseInt($(this).html()) + 1;
										if(newval <= $(this).attr("rel"))
										{
												$(this).width(newval+'%');
												$(this).html(newval+'%' );
										}

									
							   		 });


			poller_option_cc++;
		});
 /* bejs/jquery/jquery.popupWindow.js */ 

  (function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);

$('#live_pop').popupWindow({height:730,width:728,top:50,left:50, resizable:false, windowName: 'live'});


$("#poller_form").submit( function(){
								   
										var selected_choice = $("input[@name='vote_choice']:checked").val();
									   
										if(selected_choice == null)
										{
										   if($("#vote-choice:disabled").length > 0){
										 	  if($('.poller_ended').val() == 1)
										  	  		alert("انتهت صلاحية هذا التصويت");
											  else
												    alert("عذرا، لقد قمت بالتصويت مسبقا على هذا الاستفتاء");
										   }
											   	
											else
												alert("poller_please_select_an_option");
										   
										}
										
										else{
												
												$.post("poller.php", { poller_id: $("#poller_id").val(), choice: selected_choice },
														
														function(data){
																		
																		poller_res_array = data.split("|");
																		if(poller_res_array[0] != "" && poller_res_array[0] != "-")
																			alert(poller_res_array[0]);

																		if(typeof(poller_res_array[1]) != "undefined")
																			{
																				$('#poller-' + $("#poller_id").val()).html(poller_res_array[1]);
																				 animate_poller();
																				
																			}
																		

																	  });
											}
										
										
										   return false;
									} );
}



