
function initCoursesShowHideSlide() {
    
    $$('.corses_order_item_list').each( function( item, index ){

        item.addEvent( 'mouseenter', function( event ) {

            if ( item.getStyle('height').toInt() == 80 ) {
                var fx = new Fx.Styles(item, {
                    duration:200
                });
                fx.start({
                    'height': [80, 220]
                });
                $('total_price_ammount_'+index).setStyle('display', 'none'); 
                $('ajax_courses_box_'+index).setStyle('display', '');
                $('ajax_courses_cart_add_'+index).setStyle('display', '');
            }
        }); 
    });
}

var _curCourseTableIndex = 0;
function gettAjaxGetCourseTable( index ) { 

    _curCourseTableIndex = index;
    var weeksSelect = $('weeks_select'+index).value;
    var toursSelect = $('tours_select'+index).value;
    var idAccomodation = $('id_accomodation'+index).value;
    var accDateStart = $('acc_date_start'+index).value;
    var accLength = $('acc_length'+index).value;

    getAjaxHideCourseTable();

	var request_url = host_url+'/www/courses.php?act=table&id_lang='+ curlangId +'&id_cours='+ _curActArr[index] +'&id_cours_week='+weeksSelect +'&tours_select='+toursSelect+'&id_accomodation='+idAccomodation +'&acc_date_start='+accDateStart +'&acc_length='+accLength+'&index='+index;

	var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();
}

function getAjaxHideCourseTable() {

    $('ajax_courses_box_'+_curCourseTableIndex).setStyle('display', 'none');
    $('ajax_courses_cart_add_'+_curCourseTableIndex).setStyle('display', 'none');
    $('courses_order_list_course_price_'+_curCourseTableIndex).className = 'courses_order_list_course_price courses_order_list_course_loader' ;
}

function getAjaxShowCourseTable() {

    $('ajax_courses_box_'+_curCourseTableIndex).setStyle('display', '');
    $('ajax_courses_cart_add_'+_curCourseTableIndex).setStyle('display', '');
    $('courses_order_list_course_price_'+_curCourseTableIndex).className = 'courses_order_list_course_price' ;
}

function gettAjaxGetAaccLength( index ) {
    var idAccomodation = $('id_accomodation'+index).value;
	var request_url = host_url+'/www/courses.php?act=getselect&id_accomodation='+idAccomodation+'&index='+index+'&id_lang='+ curlangId;
	var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();

}

function setProGalleryDesc( index, display ) {
    $('ajax_acc_date_start_lang'+index).setStyle( 'display', display );
}

function resetLengthEvents() {

    $$('#courses_order_list .acc_length').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetCourseTable( index ); });
    });
}
var _ajaxCoursesBoxes;

function setCoursesAjaxEvents() {

    _ajaxCoursesBoxes = $$('#courses_order_list .ajax_courses_box');

    $$('#courses_order_list .weeks_select').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetCourseTable( index ); });
    });
    $$('#courses_order_list .tours_select').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetCourseTable( index ); });
    });
    $$('#courses_order_list .id_accomodation').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetAaccLength( index ); });
    });
   
   setCoursesDateStartAjaxEvents();

    $$('#courses_order_list .acc_length').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetCourseTable( index ); });
    });
}

function setCoursesDateStartAjaxEvents() {
    
    $$('#courses_order_list .acc_date_start').each( function( item, index ){
        item.addEvent( 'change', function( event ){ gettAjaxGetCourseTable( index ); });
    });
}
var curCourseSelectedId = 0;
function coursesListInit() {

    var priceList = $$('#courses_order_list .weeks_select_price');
    $$('#courses_order_list .weeks_select').each( function( item, index ){

        item.addEvent( 'change', function( event ) {
            priceList[index].setText( item.value+''+priceSymbol );
            curCourseSelectedId = index;


           var c = $('accomodation_fee_'+curCourseSelectedId).getText();
           var diskArr1 = ''+c.split('.');
           if ( diskArr1[1] ) diskArr1[1] = 0;
           var b = diskArr1[0]+''+cur_decimal_point +''+(diskArr1[1].slice(0,2));
           var d = ''+(parseFloat(item.value)+parseFloat(b));

          var discount = ''+(-1*((1-discountArr[parseInt(item.selectedIndex)])*d));

            var diskArr = discount.split('.');
            $('special_offer_'+index).setText( diskArr[0]+''+cur_decimal_point +''+(diskArr[1].slice(0,2))+priceSymbol );
            
            updateCoursePrice();

        });
    });
}

function accomodationListInit() {

    var priceList = $$('#courses_order_list .accomodation_fee');
    $$('#courses_order_list .id_accomodation').each( function( item, index ){

        item.addEvent( 'change', function( event ) {
            priceList[index].setText( item.value+''+priceSymbol );
            curCourseSelectedId = index;

//             var discount = ''+(-1*((1-discountArr[parseInt(item.selectedIndex)])*item.value));
//
//
//           var diskArr = discount.split('.');
//           var a = diskArr[0]+''+cur_decimal_point +''+(diskArr[1].slice(0,2));
//           var c = $('accomodation_fee_'+curCourseSelectedId).getText();
//
//           var diskArr1 = ''+c.split('.');
//           var b = diskArr1[0]+''+cur_decimal_point +''+(diskArr1[1].slice(0,2));
//
//           var d = ''+(parseFloat(a)-parseFloat(b));
//
//           var diskArr2 = d.split('.');
//            $('special_offer_'+index).setText( diskArr2[0]+''+cur_decimal_point +''+(diskArr2[1].slice(0,2))+priceSymbol );
//
           var c = $('weeks_select_price_'+curCourseSelectedId).getText();
           var diskArr1 = ''+c.split('.');
           if ( diskArr1[1] ) diskArr1[1] = 0;
           var b = diskArr1[0]+''+cur_decimal_point +''+(diskArr1[1].slice(0,2));
           var d = ''+(parseFloat(item.value)+parseFloat(b));

          var discount = ''+(-1*((1-discountArr[parseInt(item.selectedIndex)])*d));

            var diskArr = discount.split('.');
            $('special_offer_'+index).setText( diskArr[0]+''+cur_decimal_point +''+(diskArr[1].slice(0,2))+priceSymbol );


            updateCoursePrice();

        });
    });
}

function updateCoursePrice() {

    var price = 0;

    price = price+parseFloat($('weeks_select_price_'+curCourseSelectedId).getText());
    price = price+parseFloat($('register_fee_'+curCourseSelectedId).getText());
    price = price+parseFloat($('accomodation_fee_'+curCourseSelectedId).getText());
    price = price+parseFloat($('special_offer_'+curCourseSelectedId).getText());
    price = ''+price;

    var diskArr = price.split('.');
    $('total_price_ammount_'+curCourseSelectedId).setText( diskArr[0]+''+cur_decimal_point +''+(diskArr[1].slice(0,2)) +priceSymbol );
}

function proCommentAdd( pro, user, text, title, lang ) {

    var obj = new Base64();
	var request_url = host_url+'/www/ajax.php?comment=1&text='+ obj.encode(text)+'&title='+title+'&user='+user+'&pro='+pro+'&l='+lang;
	var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();
}


function initSchoolList(  headerTabId, tabId )
{
    $$('#courses_city_list li a').each( function( city, index ){
        city.addEvent( 'click', function( event ){
           new Event(event).stop();
           
          $$('#courses_city_list li a.active')[0].className = '';
          city.className = 'active';
           //scrollFx.toElement( 'citylist'+index );
        });
    });
}

function swapTabMenu( langSymbol )
{
    var list = $$('#box_container_content_'+curCoursesTabId+' #tab_menu_'+curCoursesTabId+' div');

    list.each( function( item, index ) {
        item.addEvent( 'click', function ( el ) {
           new Event(el).stop();

            list.each( function( item1, index1) {
                item1.className = 'tab_menu'+ (index1+1) +'_'+ langSymbol +'_unactive';
                $('tab_menu_content_'+ curCoursesTabId +'_'+(index1+1) ).setStyle('display','none');
            });

            item.className = 'tab_menu'+ (index+1) +'_'+ langSymbol +'_active';
            $('tab_menu_content_'+ curCoursesTabId +'_'+(index+1) ).setStyle('display','');
            resetChoseCourseScroll( curCoursesTabId, index+1 );
            initSchoolList( curCoursesTabId, index+1 );
        });
    });
}
function swapSearchTabMenu()
{
    var list = $$('.search_box_items #tab_menu_'+curCoursesTabId+' div');

    list.each( function( item, index ) {
        item.addEvent( 'click', function ( el ) {
           new Event(el).stop();

            list.each( function( item1, index1) {
                item1.className = 'tab_menu'+ (index1+1) +'_unactive';
                $('tab_menu_content_1_'+(index1+1) ).setStyle('display','none');
            });

            $('searchRegion').value = index;

            item.className = 'tab_menu'+ (index+1) +'_active';
            $('tab_menu_content_'+ curCoursesTabId +'_'+(index+1) ).setStyle('display','');
            resetChoseCourseScroll( curCoursesTabId, index+1 );
            initSchoolList( curCoursesTabId, index+1 );
        });
    });
}

var curCoursesTabId = 1;

function swapBoxHeaderTabs( langSymbol )
{
    var list = $$('#box_header_tabs div');

    list.each( function( item, index ) {
        item.addEvent( 'click', function ( el ) {

            new Event(el).stop();
            curCoursesTabId = index+1;

            list.each( function( item1, index1) {
                item1.className = 'course_tab_u';
                $('box_container_content_'+(index1+1) ).setStyle('display','none');
            });
            
            item.className = 'course_tab_a';
            $('box_container_content_'+curCoursesTabId ).setStyle('display','');
            swapTabMenu( langSymbol );
            resetChoseCourseScroll( curCoursesTabId, 1 );
            initSchoolList( curCoursesTabId, 1 );

        });
    });
    
    swapTabMenu( langSymbol );
    CSBfleXcroll( 'scrollArticle' );
}

function resetChoseCourseScroll( headerTabId, tabId )
{
   if ( $('scrollArticle') != undefined ) { $('scrollArticle').id = 'scrollArticle_unactive'; }
    $$('#box_container_content_'+headerTabId +' #tab_menu_content_'+headerTabId +'_'+tabId+' .scrollBoxItem').each( function(item, index){
        if ( index == 0 ) {
            item.id = 'scrollArticle';
            CSBfleXcroll( 'scrollArticle' );
        } else {
            item.id = 'scrollArticle'+index;
            CSBfleXcroll( 'scrollArticle'+index );
        }
    });
}

function initTopMenu()
{
    $$('#top_menu_list li').each( function( item, index ) {

    	var menuId = parseInt(item.className.split('_')[1]);	
        if ( item.className != 'elem_'+menuId+'a' ) {

            item.addEvent( 'mouseenter', function( el ) {
                item.className = 'elem_'+ menuId +'a';
            });
        
            item.addEvent( 'mouseleave', function(el) {
                item.className = 'elem_'+ menuId +'u';
            });
        }
    });
}

var Base64 = new Class({

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = this._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = this._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

});



