var _time = null;
var _el = false;
var _note = false;
var _sum = 0;

function innerfade() {
	if(time) clearTimeout(time);
	_curr = _note.filter(".active");
   _curr_index = _note.index(_curr);
   _next = _curr_index + 1;   
   if(_next >= _note.size()) _next = 0;
   showhide(_next,_curr_index);
   if (_sum >= 1) {
		time = setTimeout('innerfade()', 3000);
   }
};
function innerfade_prev() {
	if(time) clearTimeout(time);
	_curr = _note.filter(".active");
	_curr_index = _note.index(_curr);
	_next = _curr_index - 1;   
 	if(_next < 0) _next = _note.size() - 1;
 	showhide(_next,_curr_index);
 	if (_sum >= 1) {
		time = setTimeout('innerfade()', 3000);
 	}
};

function initPage() {//alert('iii')
	_el = $(".gallery-box .img-tab");
	_sum = _el.length;
	_note = $(".gallery-box ul li");
	
	$(".gallery-box .nav a.next").click(function(){
		innerfade();
	});
	$(".gallery-box .nav a.prev").click(function(){
		innerfade_prev();
	});
	
   var _active_cnt = 0;
   if(_note.filter(".active").size() == 1)
   {
      _active_cnt = _note.index(_note.filter(".active")[0]);
   }
   else
   {
      _note.removeClass("active");
		_note.eq(0).addClass("active");
   }
   _el.each(function(i){
      if(_active_cnt == i)
         _el.eq(i).css({'display':'block'});
      else
         _el.eq(i).css({'display':'none'});
   })

	$(_note).click(function(){
		if(time) clearTimeout(time);
      _active_cnt = _note.index(_note.filter(".active")[0]);
      _next = _note.index($(this)[0]);
		_note.removeClass("active");
		$(this).addClass("active");
      showhide(_next,_active_cnt);
	});
	
	time = setTimeout((function(){
		innerfade();
	}), 3000);
	
	/* accordion */
	//$('.info li.active div').css({ "height": 69 });
	$('.info li.active div a').css({ "height": 69 });
	$('.info .left li').not(".active").children("div").children("a").children("img").css({"opacity":0}); 
	
	$('.info .left li').hover(function() {
		if (!$(this).hasClass("active")) {
		    $('.info .left li.active').removeClass("active").children('div').children('a').animate({
				"height": 0
			}, {
				queue: false,
				duration: 300
			}).children().css({
				"opacity": 0
			});


			$(this).children('div').children("a").animate({
				height: 69
			}, {
				queue: false,
				duration: 300
			}).parent().parent().addClass("active");
			
			_fade1 = setTimeout("showImgLeft()", 350);
		}
	 }, function(){return false;});
	 
	$('.info .right li').not(".active").children("div").children("a").children("img").css({"opacity":0}); 
	
	$('.info .right li').hover(function() {
		if (!$(this).hasClass("active")) {
		    $('.info .right li.active').removeClass("active").children('div').children("a").animate({
				"height": 0
			}, {
				queue: false,
				duration: 300
			}).children().css({"opacity": "0"});

			$(this).children('div').children("a").animate({
				height: 69
			}, {
				queue: false,
				duration: 300
			}).parent().parent().addClass("active");
			
			_fade2 = setTimeout("showImgRight()", 350);
		}

	 }, function(){return false;});

	 ///alert(strLastSelectedCarDivID);
	 if (typeof (strLastSelectedCarDivID) != 'undefined' && typeof (GetLastCarItemIDSucceededCallback) != 'undefined')
	    GetLastCarItemIDSucceededCallback(strLastSelectedCarDivID);
}
var _fade1 = null;
var _fade2 = null;
function showImgLeft(){
	$('.info .left li').not(".active").find("img").css({"opacity": "0"});
	$('.info .left li.active img').animate({"opacity":"1"});
}
function showImgRight(){
	$('.info .right li').not(".active").find("img").css({"opacity": "0"});
	$('.info .right li.active img').animate({"opacity":"1"});
}

function showhide(_next,_prev){
	if(_next == _prev) return;
	 _el.eq(_next).fadeIn(600);
	_note.filter(".active").removeClass("active");
	_note.eq(_next).addClass("active");
	_el.eq(_prev).fadeOut(600);   
	_sum = _sum - 1;
}

if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

///Shows/ hides an object according to its current style display
function Toggle(objId) 
{
    //alert(objId);
    //debugger
    var obj = $get(objId);
    //alert(obj.id)
    //alert(obj.style.display)
    
    if (obj) {
        //alert(obj.style.display)

        if (obj.style.display == 'none' || obj.style.display == '')
            obj.style.display = 'block';
        else
            obj.style.display = 'none';
    }
}

///Shows/ hides an object according to its current style display
///and also replaces hrefObj' text according to objId style display
function ToggleWithText(objId, hrefObj, oldText, newText)
{   
    Toggle(objId);    

    var obj = $get(objId);

    if (hrefObj)
    
    {
        if (obj.style.display == 'none' || obj.style.display == '') {
            if (document.all) {
                hrefObj.innerText = oldText;
            }
            else {
                if (hrefObj.textContent) {
                    hrefObj.textContent = oldText;
                }            
            }
        }
        else {
            if (document.all) {
                hrefObj.innerText = newText;
            }
            else {
                if (hrefObj.textContent) {
                    hrefObj.textContent = newText;
                }
            }        
        }
        
    }
}

///toggles hrefObj inner text with oldText/ newText
function ToggleText(hrefObj, oldText, newText)
{   
//    if (hrefObj)
//    {
//        if (hrefObj.innerText == newText)
//            hrefObj.innerText = oldText;
//        else
//            hrefObj.innerText = newText;
//
    //    }
    if (hrefObj) {
        if (document.all) {
            if (hrefObj.innerText == newText)
                hrefObj.innerText = oldText;
            else
                hrefObj.innerText = newText;            
        }
        else {
            if (hrefObj.textContent) {
                if (hrefObj.textContent == newText)
                    hrefObj.textContent = oldText;
                else
                    hrefObj.textContent = newText; 
            }
        }
    }
}

///hide object by object id
function HideObject(objId) {
    //alert(typeof (obj))
    if (typeof (objId) == 'string') {
        if ($get(objId)) {
            $get(objId).style.display = 'none';
        }
    }

    if (typeof (objId) == 'object') {
        if (objId) {
            objId.style.display = 'none';
        }
    }
}

///show object by object id
function ShowObject(objId) {
    //debugger
    //alert(typeof (obj))
    if (typeof (objId) == 'string') {
        if ($get(objId)) {
            $get(objId).style.display = 'block';
        }
    }

    if (typeof (objId) == 'object') {
        //alert(objId != null)
        if (objId) {
            objId.style.display = 'block';
            //alert(objId.style.display)
        }
    }
}

function findPosCurtop(obj) {
    var curtop = 0;
    if (obj != null) {
        if (obj.offsetParent) {
            curtop = obj.offsetTop;
            while (obj = obj.offsetParent) {
                curtop += obj.offsetTop;
            }
        }
    }
    //alert('return curtop ' + curtop);
    curtop += document.body.scrollTop;

    return curtop;
} 

///true if the style.display of obj is different from 'none'
function IsVisible(obj) { 
    var result = false;

    if (obj) {
        result = (obj.style.display != 'none');
    }

    return result;    
}


function SetTxtSpan(p_Context) {
    var clientID = null;
    
    if (p_Context == "IsraelHotels")
        clientID = clientIDIsraelHotels;

    if (p_Context == "Hotels")
        clientID = clientIDHotels;
    //alert(p_Context)
    if (eval("PickerFrom" + p_Context) && eval("PickerTo" + p_Context)) {
        var fromDate = Date.parse(eval("PickerFrom" + p_Context).getSelectedDate()); //document.getElementById("TextBox1").value;
        var tillDate = Date.parse(eval("PickerTo" + p_Context).getSelectedDate()); //document.getElementById("TextBox2").value;

        var one_day = 1000 * 60 * 60 * 24;
        var _Diff = Math.ceil((tillDate - fromDate) / (one_day));
        var _TxtSpan = document.getElementById(clientID + "_TxtData");

        if (_TxtSpan != null) {
            _TxtSpan.value = _Diff;
        }
    }
    
    

}

//function SetCalFromTxtBx(val, p_Context) {
//    //debugger;
//     alert(val.value);
//    if (eval("PickerFrom" + p_Context) && eval("PickerTo" + p_Context)) { 
//        var NewDate = new Date(eval("PickerFrom" + p_Context).getSelectedDate());
//        alert(NewDate);
//        NewDate.setDate(parseInt(NewDate.getDate()) + parseInt(val.value));
//        eval("PickerTo" + p_Context).setSelectedDate(NewDate);
//    }


//}

///returns true if obj != "undefined"

///validation function for non ie browsers because custom validators do not work in ff
///the value of this function should be the return value of the client click event of the wanted submit button
///i.e. "return NonIEValidation()"
function NonIEValidation() {
    ///for ff
    if (window.addEventListener) {
        return ValidateSearchFormCars(null, null);
    }
    else
        return true;
}

function EmailOrPhoneInfo(oSrc, args) {
    //debugger;
    //alert('');
    //debugger;
    //alert(document.getElementById('ctl00_PhoneTextBox').value);
    if ((document.getElementById('ctl00_ContentHolder_InformationRequest1_PhoneTextBox').value.length == 0) && (document.getElementById("ctl00_ContentHolder_InformationRequest1_EmailTextBox").value.length == 0))
        args.IsValid = false;
    else
        args.IsValid = true;

}

///redirects to same page with qs indicating conversion to shekel or vise versa
function RedirectToShekelPage(bConvertToShekel) {
    //debugger
    //location.href = "sdfds";
    //location.replace('dfgdf');
    //return;
    //alert()
    var locationHref = location.href;

    var convertToShekelUrl = locationHref.replace('&cts=1', '');
    convertToShekelUrl = convertToShekelUrl.replace('&cts=0', '');

    if (bConvertToShekel) {
        location.href = convertToShekelUrl + '&cts=1';
        //return;
    }
    else {
        location.href = convertToShekelUrl + '&cts=0';
        //location.href = 'asdas';
        //return;
    }
}

///Checks if the ShowMessageForRedirectingFromWLToDefaultSite function exists and calls it
function ShowMessageForRedirectingFromWLToDefaultSite_Proxy(obj) {
//    if (typeof (ShowMessageForRedirectingFromWLToDefaultSite) == 'function')
//        ShowMessageForRedirectingFromWLToDefaultSite(obj);
}


//function VlidateEmailField(source, args) {

//    //debugger;
//    //alert('address');
//    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//    var address = args.Value; //document.getElementById("ctl00_ContentHolder_UserOrderDetails1_tbxEmail").value;

//    if (address.length == 0) {
//        source.innerHTML = "כתובת מייל חסרה"
//        args.IsValid = false;
//    }
//    else {
//        if (reg.test(address) == false) {
//            //alert('Invalid Email Address');
//            source.innerHTML = "כתובת מייל לא חוקית"
//            args.IsValid = false;
//        }
//        if (reg.test(address)) {
//            args.IsValid = true;
//        }

//    }
//}


//function ValidateReservationNumber(source, args) {

//    var ReservationNumber = args.Value;
//    if (ReservationNumber.length == 0) {
//        source.innerHTML="יש להזין מספר הזמנה";
//        args.IsValid = false;
//    }

//    if (!isNaN(ReservationNumber)) {
//        source.innerHTML = "נא להזין מספר";
//    }
//    args.IsValid = (!isNaN(ReservationNumber));
//}
