/*===========Reza===============*/
var $j = jQuery.noConflict();

function imgBed(x,y,z){this.intervalId = x; this.curBnr = y; this.total = z}
var rotateObj = new imgBed(0,0,0), rot_speed=5000;

//preload images:
var backImages= new Array('hdr_bg.jpg','footer_bg.jpg'); /*Add as many*/
function preLoadBgImages(){
	var pre_img_obj = new Array(backImages.length);
	for(i=0; i<backImages.length; ++i){
		pre_img_obj[i] = document.createElement('img');
		pre_img_obj[i].src = 'images/backgrounds/'+backImages[i];
	}
}

jQuery(document).ready(function(){
	//set from options by object (if available):
	if( typeof(st_theme_vars) != 'undefined' && typeof(st_theme_vars.rot_speed) != 'undefined' && st_theme_vars.rot_speed ){
		rot_speed = parseInt(st_theme_vars.rot_speed);
	}
	
	//navigation:
	jQuery("#topNav > li").hover(function(){
			$j(this).find('ul').stop(true,true).slideDown('fast');
		},
		function(){
			$j(this).find("ul:not('li.quoted ul')").slideUp('fast');
		}
	);
	jQuery("#topNav > li li a.butn_close").click(function(){
		$j('#topNav > li.quoted ul').slideUp('fast');
	});
	
	//image rotator:
	rotateObj.total = $j('.rotateCont img').length;
	if(rotateObj.total >0){
		$j('.rotateCont img:eq('+rotateObj.curBnr+')').css('display','block');
		if(rotateObj.total >1) playBnr();
	}
	
	//image rotator:
	$j('#rotateContThumb img').each(function(index){
		//click events:
		/*$j(this).click(function(){
			pauseBnr();
			changeBnr(index);
			playBnr();
		});*/
		
		//hover events:
		$j(this).hover(function(){pauseBnr(); changeBnr(index)},function(){playBnr()});
	});
});

function delayer(url){window.location = url}

//==============::ROTATION::====================
function pauseBnr(){clearInterval(rotateObj.intervalId)}
function playBnr(){rotateObj.intervalId=setInterval('startBnrPlay()', rot_speed)}
function startBnrPlay(){
	(rotateObj.curBnr==(rotateObj.total-1))? rotateObj.curBnr=0 : rotateObj.curBnr++;
	changeBnr(rotateObj.curBnr);
}
function changeBnr(index){
	$j('.rotateCont img').stop(true,true);
	$j('.rotateCont img:not(:eq('+index+'))').fadeOut('slow');
	$j('.rotateCont img:eq('+index+')').fadeIn('slow');
}



//===========::FORM VALIDATIONs::===============
function checkEmail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(email)) return true; else return false;
}

function chkValidity(form, location){
	var reqFields = jQuery(form).find(".mendatory");
	var err=false;
	
	//check for empty fields:
	for(i=0; i < reqFields.length; i++){ // ERR = NULL or ERR_MSG or DEFAULT_SELECT
		if( reqFields[i].value=='' || (reqFields[i].value==0 && reqFields[i].tagName.toUpperCase()=='SELECT') ){//SELECT TAG but has initial value to '0':
			err=true;
			if( jQuery(reqFields[i]).attr('type') == 'hidden' ){
				err = false; continue; //do nothing;
			}
			else if( jQuery(reqFields[i]).attr('type') == 'file' )
				alert("Please upload image file(s).");
			else if( reqFields[i].tagName.toUpperCase() == 'SELECT' ){//SELECT TAG but no initial value:
				alert('Please complete selecting [ '+jQuery(form).find("label[for='"+reqFields[i].name+"']").text()+' ]');
			}
			else{
				var alertMsg = (location=='sidebar')? 'Please complete all fields' : 'Please complete the required fields marked with (*)';
				alert(alertMsg);
			}
			break;
		}
		
		if(jQuery(reqFields[i]).hasClass('email')){
			if( !checkEmail( reqFields[i].value ) ){
				err=true;
				alert("Please enter a valid Email address");
				break;
			}
		}
	}
	return (err)? false : true;
}
