var reqDone;

/*--------------------------------------------------------------------------------*\
|	getCookie
|----------------------------------------------------------------------------------
| Returns cookie with given name.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		name	string	- Cookie name to search.
|	Return:
|			string	- Cookie value.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function getCookie (name) {

	var cookieValue	= "";
	var cookieName	= name + "=";
	
	if ( document.cookie.length > 0 ) { 
	
		offset = document.cookie.indexOf(cookieName);
		if ( offset != -1 ) { 
		
			offset	+= cookieName.length;
			
			end	= document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			
			cookieValue = unescape(document.cookie.substring(offset, end))
		
		} //IF found smth
	
	} //IF there are cookies
	
	return cookieValue;
} //FUNC getCookie

/*--------------------------------------------------------------------------------*\
|	setCookie
|----------------------------------------------------------------------------------
| Sets cookie with given name.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		name	string	- Cookie name to set.
|		value	string	- Value of cookie.
|		[hours]	string	- Expiration date in hours.
|	Return:
|		VOID
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function setCookie(name, value, hours) {
	
	var expire	= "";
	
	if ( hours ) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	} //IF there is set expiration
	
	document.cookie = name + "=" + escape(value) + expire;
} //FUNC setCookie


/*--------------------------------------------------------------------------------*\
|	PrintPage
|----------------------------------------------------------------------------------
| Initiates "Print page" dialog.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		content	string	- Content element ID.
|	Return:
|			bool	- Always false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function PrintPage(content) {
	
	var el = document.getElementById(content);
	if ( !el ) return false;
	
	var text = el.innerHTML;
	
	win=window.open('#','printPage','toolbar=0, status=0, width=320, height=400, scrollbars=1');
	win.document.write(text);
/**/	
	win.document.close();  
	win.focus();  
	win.window.print(); 
/*/
	win.print();
/**/
	return false;
} //FUNC PrintPage

/*--------------------------------------------------------------------------------*\
|	sendToFriend
|----------------------------------------------------------------------------------
| Initiates "Send to Friend" dialog.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		event	object	- Callback event object.
|	Return:
|			bool	- Always false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function sendToFriend (event) {
	window.open('/sendfriend/','sendToFriend','toolbar=0,status=0,width=320,height=400');
	event.cancel=true;
	return false;
} //FUNC sendToFriend

/** //** ----= CLASS mwLng		 =----------------------------------------------\**//** \
*
* 	Languadges switching tools
*
* 	@package	MorwebManager
* 	@subpackage	JS frontend
* 	@category	helper
*
\**//** --------------------------------------------= by Mr.V!T @ Morad Media Inc. =----/** //**/
var mwLng = {
	
	/** //** ----= init 	 =------------------------------------------------------\**//** \
	*
	* 	Proceeds startup initiation.
	*
	* 	@return	SELF   
	*
	\**//** ------------------------------------= by Mr.V!T @ Morad Media Inc. =----/** //**/
	init	: function () {

		var obj	= this;
		
		var lng	= getCookie('mwSiteLng') || 'en';

		// Setting small delay to allow sifr to start init
		// otherwise it just ignores invisible elements though they are pointed to it
		setTimeout( function () {
			obj.set(lng);
		}, 10);

		return this;
	}, //FUNC init
	
	/** //** ----= set 	 =------------------------------------------------------\**//** \
	*
	* 	Shows given languadge, hiding others.
	*
	*	@param	string	set	- Language shortcut.
	*
	* 	@return	SELF   
	*
	\**//** ------------------------------------= by Mr.V!T @ Morad Media Inc. =----/** //**/
	set	: function (lng) {

		jQuery('[lang=' + lng + ']').show();
		jQuery('[lang][lang!=' + lng + ']').hide();

		// Setting for 12 Month
		setCookie('mwSiteLng', lng, 720*12);

		return this;
	} //FUNC set
	
} //CLASS mwLng

if (jQuery) {
	
	jQuery(function () {
		
		mwLng.init();
		
		jQuery('FORM.sectionForm').submit(function () {
			
			var tform = this;
			
			var res = validateForm(this, {FailClass : 'inputError'});
			
			/**/

			if ( res ) {
				
				jqmShowCentered('#AJAXLoader', 'fade');
				jQuery(tform).hide();
				
				AR = new ajaxReturn('','/postForm');
				AR.onSuccess = function (data) {
					
					jqmHide('#AJAXLoader');
					jQuery(tform).replaceWith('<div>' + data.res + '</div>');
					
					if ( data.redirect )
						document.location.href = data.redirect;

				}; //FUNC on success
				AR.onSimple = AR.onError = AR.onFail = function (data) {
					jqmHide('#AJAXLoader');
					jQuery(tform).replaceWith('<div>' + data._message + '</div>');
				}; //FUNC on fail
				AR.postForm(tform);
				
			} //IF validation passed
			
			return false;
			/*/
			
			return res;
			
			/**/
		}); //jQuery each form
	
	}); //jQuery onLoad
	
	jQuery('.sendFriend').click(sendToFriend);
	jQuery('.printPage')
		.unbind('click')
		.click( function () {
		
		var rel = jQuery(this).attr('rel');
		
		if ( rel )
			PrintPage(rel);
		
		return false;
	}); //jQuery .printpage click 
	
} else {
/**/	
    $$('form').each( function (item) {
        item.observe('submit', function (event) {
            item.$$('.required').each (
                function (item) {
                    if (!item.value) {
                        item.setStyle( { backgroundColor: 'yellow' } );
                        Event.stop(event);
                        if (!reqDone) {
                            reqDone=true;
                            alert('Please complete the required fields [marked in yellow]');
                        }
                    }
                }
            );
        });
    });

    $$('.sendFriend').each( function (item) {
        item.observe('click', function (event) {
            window.open('/sendfriend/','sendToFriend','toolbar=0,status=0,width=320,height=400');
            event.cancel=true;
        });
    });

    $$('.printPage').each( function (item) {
        item.observe('click', function (event) {
            item=Event.element(event);
            rel=item.getAttribute('rel');
            win=window.open('#','printPage','toolbar=0,status=0,width=320,height=400');
            win.document.write($(rel).innerHTML);
            win.print();
            event.cancel=true;
        });
    });
    /**/
} 


