/*MASPHP.Com written Copyright 2008-2009 */
/*ajax Starter
----------------------------------------------------------------*/
var xmlhttp = false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}



function show(sline,spart){
for(var i=1;i<=spart;i++){
  var one=document.getElementById(sline+'_'+i);
  var two=one.setAttribute("src","images/s/star0.png");
}
spart++;
for(var i=spart;i<=5;i++){
  var one=document.getElementById(sline+'_'+i);
  var two=one.setAttribute("src","images/s/star1.png");
}

}
function hide(sline,spart){
for(var i=1;i<=spart;i++){
  var one=document.getElementById(sline+'_'+i);
  var two=one.setAttribute("src","images/s/star0p.png");
}
spart++;
for(var i=spart;i<=5;i++){
  var one=document.getElementById(sline+'_'+i);
  var two=one.setAttribute("src","images/s/star1p.png");
}

}


function ajax_rateit(rval,sid,rspan){
  var rspanobj= document.getElementById(rspan);
  rspanobj.innerHTML='در حال ارسال ...';
  xmlhttp.open("GET", 'ajaxhandler.php?rate_uf=true&value='+rval+'&id='+sid);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	  rspanobj.innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}


/*--- jquery ----------------------*/
$(document).ready(function(){
	//--- window min size
	//$('div#mastercontent.otherpage div#content').css({'min-height':$(window).height()-220});
	//alert($(window).height());
	//---- master menu
	$('div#nav_menu a.active').closest('li').addClass('active');
	$('div#nav_menu li ul').closest('li').addClass('haschild');
	$('div#nav_menu ul li').hover(
	function(){
		$(this).find('>ul').stop(true,true).animate({"opacity": "show","width": "show"},300);
		$(this).addClass('hover').find('>a').addClass('hover');
	},function(){
		$(this).find('>ul').stop(true,true).animate({"opacity": "hide","width": "hide"},300);
		$(this).removeClass('hover').find('>a').removeClass('hover');
	});
	//---- form fields
	$('input[type=text],textarea,select').addClass('formtextfield').live('mouseover',
	function(){
		$(this).addClass('formtextfield_hov');
	}).live('mouseout',function(){
		$(this).removeClass('formtextfield_hov');
	}).live('focus',function(){
		$(this).addClass('formtextfield_foc').removeClass('formtextfield_error');
	}).live('blur',function(){
		$(this).removeClass('formtextfield_foc');
	});
		
	$(':button,:submit,:reset').addClass('formbutton').live('mouseover',
	function(){
		$(this).addClass('formbutton_hov');
	}).live('mouseout',function(){
		$(this).removeClass('formbutton_hov');
	});
	
	//--- contactus form
	$('#contactus_form').submit(function(){
		var f_list=$(this).find('input[type=text],textarea');
		
		for(var i=0;i<f_list.length;i++){
			if($(f_list[i]).hasClass('i_txt_mand') && f_list[i].value==''){
				$(f_list[i]).addClass('formtextfield_error');	
			}else{
				$(f_list[i]).removeClass('formtextfield_error');
			}
		}
		
		if($(this).find('.formtextfield_error').length>0){
			alert('موارد مشخص شده بایستی پر شوند');
			return false;
		}
        
	});
	$('form.form_precheck').submit(function(){
		var f_list=$(this).find('input[type=text],input[type=password],textarea');
		
		for(var i=0;i<f_list.length;i++){
			if($(f_list[i]).hasClass('i_txt_mand') && f_list[i].value==''){
				$(f_list[i]).addClass('formtextfield_error');	
			}else{
				$(f_list[i]).removeClass('formtextfield_error');
			}
		}
		
		if($(this).find('.formtextfield_error').length>0){
			alert('موارد مشخص شده بایستی پر شوند');
			return false;
		}
        
	});
	//----pagemkr_mvr
	if($('div#pagemkr_mvr')[0]){
		//values
		pagemkr_mvr_frame_width=$('div#pagemkr_mvr_frame').width();
		pagemkr_mvr_cnt_li=$('div#pagemkr_mvr_frame li').length;
		pagemkr_mvr_cnt_div=$('div#pagemkr_mvr_frame ul').length;
		pagemkr_mvr_each_divli=$('div#pagemkr_mvr_frame div#pagemkr_mvr_p0 li').length;
		if($('div#pagemkr_mvr_frame li.active')[0])pagemkr_mvr_current_div=parseInt($('div#pagemkr_mvr_frame li.active').closest('div.pagemkr_mvr_p')[0].id.match(/[0-9]+/)); 
		else pagemkr_mvr_current_div=0;
		//
		$('div#pagemkr_mvr').prepend('<a href="javascript:;" id="pagemkr_mvr_bnext"></a>').prepend('<a href="javascript:;" id="pagemkr_mvr_bback"></a>');
		$('div.pagemkr_mvr_p').css('right',pagemkr_mvr_frame_width);
		$('div#pagemkr_mvr_p'+pagemkr_mvr_current_div).css('right',0);
		pagemkr_mvr_btstatus();
		//
		$('a#pagemkr_mvr_bnext').click(function(){
			if(pagemkr_mvr_current_div>=(pagemkr_mvr_cnt_div-1))return false;
			$('div.pagemkr_mvr_p').stop( false, true );
			$('div#pagemkr_mvr_p'+pagemkr_mvr_current_div).animate({'right':(pagemkr_mvr_frame_width*-1)},800);
			$('div#pagemkr_mvr_p'+(pagemkr_mvr_current_div+1)).css('right',pagemkr_mvr_frame_width).animate({'right':0},800);
			pagemkr_mvr_current_div++;
			pagemkr_mvr_btstatus();
		});
		$('a#pagemkr_mvr_bback').click(function(){
			if(pagemkr_mvr_current_div<=0)return false;
			$('div.pagemkr_mvr_p').stop( false, true );
			$('div#pagemkr_mvr_p'+pagemkr_mvr_current_div).animate({'right':pagemkr_mvr_frame_width},800);
			$('div#pagemkr_mvr_p'+(pagemkr_mvr_current_div-1)).css('right',(pagemkr_mvr_frame_width*-1)).animate({'right':0},800);
			pagemkr_mvr_current_div--;
			pagemkr_mvr_btstatus();
		});
	}
	
	//news letter
	if($('div#nletter_box').length>0){
		$('div#nletter_box input#nletter_email').focus(function(){ $(this).addClass('active'); }).blur(function(){ if($(this).val()=='')$(this).removeClass('active'); });
		
		$('div#nletter_box input[name="nletter_remove"]').click(nletter_remove);
		$('div#nletter_box input[name="nletter_add"]').click(nletter_add);
		$('div#nletter_box form').submit(function(){return false;});
	}
	
	//--Add this code
	$('div#addthis_code').html('<!-- AddThis Button BEGIN --><div class="addthis_toolbox addthis_default_style "><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4d78b48c06c3d7b7"></script><!-- AddThis Button END -->');
	//--
	
});


/*--- nletter ----------------------*/
function nletter_post(addstr){
	if(addstr!='')addstr='&'+addstr;
	
	var email=$('div#nletter_box input[name="nletter_email"]').val();
	if(email==''||email==undefined){alert('ایمیل شما خالی است'); return''; }
	if(/.+@.+\..+/.test(email)!=true){alert('ایمیل شما نادرست است'); return''; }
	
	//
	$('div#nletter_box').animate({opacity:'0'}, 500,function(){
		$(this).css({'visibility':'hidden'}).load('ajaxhandler.php?nletter&nletter_email='+email+addstr, function() {
			$(this).css({'visibility':'visible'}).animate({opacity:'1'}, 500).closest('div.men_round_box').removeClass('loading_jquery');
		});
	}).closest('div.men_round_box').addClass('loading_jquery');
}
function nletter_remove(){
	nletter_post('nletter_remove');
}
function nletter_add(){
	nletter_post('');
}

//other functions
function pagemkr_mvr_btstatus(){
	if(pagemkr_mvr_current_div>=(pagemkr_mvr_cnt_div-1))$('a#pagemkr_mvr_bnext').hide(); else $('a#pagemkr_mvr_bnext').show();
	if(pagemkr_mvr_current_div<=0)$('a#pagemkr_mvr_bback').hide(); else $('a#pagemkr_mvr_bback').show();
}


//auto complete Name Suggest ------------------------------------------
function cn_show(){
	if(document.getElementById('cn_listofsuggest').innerHTML!='')document.getElementById('cn_listofsuggest').style.display='';
}
function cn_hide(){
	document.getElementById('cn_listofsuggest').style.display='none';
	//setTimeout('document.getElementById(\'cn_listofsuggest\').style.display=\'none\';',100);
}
function cn_put_value(val){
	cn_get_list(val);
	document.getElementById('cn_name').value=val;
}
function cn_show_list(val,ary){
	document.getElementById('cn_listofsuggest').className='';
	var outstr='';
	for(var i=0, j=0;i<ary.length && j<8;i++)if(ary[i].indexOf(val)==0||ary[i].indexOf(' '+val)!=-1){
		outstr+='<li><a href="javascript:cn_put_value(\''+ary[i]+'\'); void(0);">'+ary[i]+'</a></li>';
		j++;
	}
	if(outstr!='')document.getElementById('cn_listofsuggest').innerHTML='<ul>'+outstr+'</ul>';
	else{
		document.getElementById('cn_listofsuggest').innerHTML='';
		cn_hide();
	}
}
var suggest_name_array=new Array();
function cn_get_list(val){//max result = 20;
	 val=val.replace(/^[\s]+/,'').replace(/[\s]{2,}/,' ').toLowerCase();
	if(val==''){
		document.getElementById('cn_listofsuggest').innerHTML='';
		cn_hide();
	}
	else{
		document.getElementById('cn_listofsuggest').innerHTML='<p>&nbsp;</p>';
		cn_show();
		//
		if(typeof(suggest_name_array[''+val])!='undefined')cn_show_list(val,suggest_name_array[''+val]);
		else {
			for(i=(val.length-1);i>0;i--)if(typeof(suggest_name_array[''+val.substring(0,i)])!='undefined')if(suggest_name_array[''+val.substring(0,i)].length<20){cn_show_list(val,suggest_name_array[''+val.substring(0,i)]); return null; }
			//if not found or has more than 20 on parrent get new list
			//get ajax
			//suggest_name_array[''+val]=new Array('sdsd','sbob');
			//alert('new array');
			xmlhttp.abort();
			
			params = 'get_suggestlist=true&val='+val+'';
			xmlhttp.open("POST", 'ajaxhandler.php',true);
			//Send the proper header information along with the request
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.setRequestHeader("Connection", "close");
			document.getElementById('cn_listofsuggest').className='loading';
			
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					suggest_name_array[''+val]=new Array();
					if(xmlhttp.responseText!='Error'&&xmlhttp.responseText!=''){
						suggest_name_array[''+val]=xmlhttp.responseText.split('&');
						for(var j in suggest_name_array[''+val])suggest_name_array[''+val][j]=(suggest_name_array[''+val][j]).toLowerCase();
					}
					cn_show_list(val,suggest_name_array[''+val]);
				}
			}
			xmlhttp.send(params);
			//done
			
		}
	}
}




/*--- vote cast ----------------------*/
function votecast_start(){
	$('div#votecast_show div.votecast_value').wrap('<div class="votecast_valwrap" />').before('<div class="votecast_valshow" />');
	$('div#votecast_show').animate({opacity:'1'}, 1000,function(){ votecast_animateresult(); });
	//votecast_animateresult();
}
function votecast_animateresult(){
	$('div#votecast_show div.votecast_valshow').stop(true,false);
	
	var vote_sum=0;
	$('div#votecast_show div.votecast_value').each(function() {vote_sum+=parseInt($(this).html()); });
	if(vote_sum<=0)vote_sum=1;
	//
	$('div#votecast_show div.votecast_value').each(function() {
		var resps=parseInt(parseInt($(this).html())/vote_sum*100);
		if(resps<=0)resps=1;
		$(this).closest('div.votecast_valwrap').find('div.votecast_valshow').animate({width:resps+'%'}, 1000);
	});
	
}
function votecast_option_over(){
	$(this).addClass('hover').find('div.votecast_value').html(parseInt($(this).find('div.votecast_value').html())+1);
	votecast_animateresult();
}
function votecast_option_out(){
	$(this).removeClass('hover').find('div.votecast_value').html(parseInt($(this).find('div.votecast_value').html())-1);
	votecast_animateresult();
}
function votecast_option_click(){
	if($(this).closest('ul.votecast_options').hasClass('votecast_notlogin')){
		alert('برای افزودن رای خود ، لطفا ابتدا با اکانت خود وارد شوید');
		return false;
	}
	//
	var oid=$(this).attr('id').match(/[0-9]+/);
	var vid=$(this).closest('ul.votecast_options').attr('id').match(/[0-9]+/);
	$('div#votecast_show').animate({opacity:'0'}, 500,function(){
		$(this).css({'visibility':'hidden'}).load('./ajaxhandler.php?votecast&addvote&votid='+vid+'&oid='+oid, function() {
			$(this).css({'visibility':'visible'}).closest('div.master_box_cont').removeClass('loading_jquery');
			votecast_start();
		});
	}).closest('div.master_box_cont').addClass('loading_jquery');
	
}

$(document).ready(function(){
	//vote cast starter
	if($('div#votecast_show').length>0){
		$('div#votecast_show').css({opacity:'0'});
		votecast_start();
		if($('div#votecast_show li.votecast_option_selected').length==0){
			$('div#votecast_show li.votecast_option').hover(votecast_option_over,votecast_option_out);
			$('div#votecast_show li.votecast_option').click(votecast_option_click);
		}
	}
});

/* home page tabs */
var hm_tb_intid=0;
var hm_tb_index=0
function hm_tb_show(index){
	$('ul#hm_tb_head li.selected').removeClass('selected');
	$('div#hm_tb_body div.selected').removeClass('selected').animate({opacity:'hide',height: 'hide'},1000);
	
	$($('ul#hm_tb_head li').get(index)).addClass('selected');
	$($('div#hm_tb_body div.hm_tb_frame').get(index)).addClass('selected').animate({opacity:'show',height: 'show'},1000);
}
function hm_tb_show_next(){
	var c_index=0;
	if($('ul#hm_tb_head li.selected').length>0)c_index=$('ul#hm_tb_head li.selected').index();
	
	if(($('ul#hm_tb_head li').length-1)>c_index)hm_tb_show(c_index+1);
	else hm_tb_show(0);
}
$(document).ready(function(){
	$('ul#hm_tb_head li').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	}).click(function(){
		hm_tb_show($(this).index()); 
		clearInterval(hm_tb_intid);
	});
	
	$('div#hm_tb_body div.hm_tb_frame').hide();
	
	if($('ul#hm_tb_head li').length>0){
		hm_tb_show(0);
		hm_tb_intid=setInterval(function(){hm_tb_show_next()},6000);
	}
});

