/**
 * This software is commercial and protected under copyright law
 * This software was written to run on www.brazilbronze.com ONLY
 *
 * @package www.brazilbronze.com
 * @subpackage slider-v2.js
 * @copyright Hamidof.com 2007
 * @version 1.0
 * @author Hamid Alipour <hamid@hamidof.com>
**/
var version_full = navigator.userAgent;
var l_anim 			= null;
var r_anim			= null;
var moving_left	= false;
var moving_right	= false;
var motion_speed	= null;
function get_motion_speed(distance) {
	//alert(distance);
	distance = distance < 0 ? -1 * distance : distance;
	//alert(distance);
	return (distance * 7) / 500;
}
function px_to_int(string) {
	var y = string.split("p"); 
	return y[0];
}
function move_left() {
	if( l_anim == null ) {
		stop_motion();
		if (version_full.indexOf('Safari')<0){
     		var left = px_to_int(YAHOO.util.Dom.getStyle('press-covers-slides', 'left'));
     	}
     	else {
     		var pos = GetPos('press-covers-slides');
			var left = pos.left;
     	}
		motion_speed   = get_motion_speed((-1 * press_covers_motion_width) - left);
		var attributes = {   
							left: { to: -1 * press_covers_motion_width }
						 };   
		l_anim = new YAHOO.util.Anim('press-covers-slides', attributes, motion_speed, YAHOO.util.Easing.easeNone);
		l_anim.onComplete.subscribe(move_right_delay, this);
		l_anim.animate();
	}
}
function move_right() {
	if( r_anim == null ) {
		stop_motion();
		if (version_full.indexOf('Safari')<0){
     		var left = px_to_int(YAHOO.util.Dom.getStyle('press-covers-slides', 'left'));
     	}
     	else {
     		var pos = GetPos('press-covers-slides');
			var left = pos.left;
     	}
     	var motion_speed   = get_motion_speed(left);
		var attributes = {   
							left: { to: 0 }
						 };   
		r_anim = new YAHOO.util.Anim('press-covers-slides', attributes, motion_speed, YAHOO.util.Easing.easeNone);
		r_anim.onComplete.subscribe(move_left_delay, this);
		r_anim.animate();
	}	
}
var __left_to  = null;
var __right_to = null;
var __delay		= 1000;
function move_left_delay() {
	clearTimeout(__left_to);
	__left_to = setTimeout('move_left()', __delay);
}
function move_right_delay() {
	clearTimeout(__right_to);
	__right_to = setTimeout('move_right()', __delay);
}
function slider_init() {
	press_covers_div 				= get_object('press-covers-slides');
	press_covers_motion_width 	= press_covers_div.offsetWidth - get_object('press-covers-box').offsetWidth;
	YAHOO.util.Dom.setStyle('press-covers-slides', 'left', -1 * press_covers_motion_width);
	move_right_delay();
}
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}		
function fixOnMouseOut(element, event, JavaScript_code) {			
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		eval(JavaScript_code);
	}
}
function stop_motion() {				
	if( l_anim != null ) {
		if( l_anim.isAnimated() ) YAHOO.util.AnimMgr.stop(l_anim);
		l_anim = null;
	} 
	if( r_anim != null ) {
		if( r_anim.isAnimated() ) YAHOO.util.AnimMgr.stop(r_anim);
		r_anim = null;
	} 
}
function start_motion(event) {
	if( !window_loaded ) return;
	var window_width 	= get_window_width();
	var mouse_x_pos  	= typeof(event.offsetX) != 'undefined' ? event.clientX : event.pageX;
	var press_box_width	= get_object('press-box').offsetWidth;
	var margin_left		= (window_width - press_box_width) / 2;
	if( mouse_x_pos < margin_left + (press_box_width / 2) ) {
		move_left();
	} else {
		move_right();
	}
}
function get_window_width() {
	var window_width = null;
	if( typeof(window.innerWidth) != 'undefined' ) {
		window_width = window.innerWidth;
	} else if( typeof(window.clientWidth) != 'undefined' ) {
		window_width = window.clientWidth;
	} else if( typeof(document.body.offsetWidth) != 'undefined' ) {
		window_width = document.body.offsetWidth;
	}
	return window_width;
}
function get_object(id)
{
        if (document.getElementById)
        {
                return document.getElementById(id);
        }
        else if (document.all)
        {
                return document.all[id];
        }
        else if (document.layers)
        {
                return document.layers[id];
        }
        else
        {
                return null;
        }
}
function GetPos(elem){
var offTrial=document.getElementById(elem);
var offL=0;
var offT=0;

while(offTrial)
{
offL+=offTrial.offsetLeft;
offT+=offTrial.offsetTop;
offTrial=offTrial.offsetParent;
}

if (navigator.userAgent.indexOf("Mac")!=-1 && typeof document.body.leftMargin!="undefined")
{
offL+=document.body.leftMargin;
offT+=document.body.topMargin;
}
return {left:offL , top:offT}
}