var tab_mode = 'view';
var site_url = 'http://www.alexandriatravel.bg/';

//store here variables if tab is updated
var Etab_id;
var Epid;
var Etype;
var Eext_id;
var Eupdate = false;

function gv(elementId)
{
	//alert(document.getElementById(elementId).value);
	if (document.getElementById(elementId) != undefined)
	{
		return  document.getElementById(elementId).value;
	}else
	{
		return false;
	}
} //gv

function sv(elementId,value)
{
	if (document.getElementById(elementId) != undefined)
	{
		document.getElementById(elementId).value=value;
	}else
	{
		return false;
	}
} //sv

function ge(elementId)
{
	if (document.getElementById(elementId) != undefined)
	{
		return  document.getElementById(elementId);
	}else
	{
		return false;
	}
} //ge

function isEnterKey()
{
   if (event && event.keyCode == 13) { return true; } else { return false; }
} //isEnterKey

function shb(what)
{
   if(document.getElementById(what).style.display=="none" || document.getElementById(what).style.display=="")
   {
     document.getElementById(what).style.display="block";

   } else {
     document.getElementById(what).style.display="none";
   }
} //shb

function hide_el(what)
{
	if(document.getElementById(what)!= undefined && document.getElementById(what)!= '')
	{
		 try  { document.getElementById(what).style.display="none"; } catch(ex) { }//catch

	}
} //hide_el

function hide_class(what)
{
	var arr = document.getElementsByClassName(what);
	if(arr!= undefined && arr!= '')
	{
		 for (var k = 0, l = arr.length; k < l; k++)
		 {
			 arr[k].style.display="none";
		 }  //for
	}//if
}//hide_class

function show_class(what)
{
	var arr = document.getElementsByClassName(what);
	if(arr!= undefined && arr!= '')
	{
		 for (var k = 0, l = arr.length; k < l; k++)
		 {
			 arr[k].style.display="";
		 }  //for
	}//if
} //show_class

function show_el(what)
{
 if(document.getElementById(what)!= undefined && document.getElementById(what)!= '')
 {
     try { document.getElementById(what).style.display="block"; } catch(ex) { }//catch
 }//if
}//function

function activate_tab(what,cont_id)
{
	var tabber = ge('tabscontholder');
	var act_tab = ge(what);
	var cont_tab = ge(cont_id);
	var temp;
	
	if (!document.getElementsByTagName) { return false; }
	
	/* Loop through an array of all the child nodes within our tabber element. */
	childNodes = tabber.childNodes;
	for(i=0; i < childNodes.length; i++){
		/* Find the nodes where class="tcont" */
		if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tcont(\\s|$)'))){
			childNodes[i].style.display = 'none'; 
		}
	}//for
	
	
	tabber = ge('tbclst');
	
	/* Loop through an array of all the child nodes within our tabber element. */
	childNodes = tabber.childNodes;
	for(i=0; i < childNodes.length; i++){
		/* Find the nodes where class="tabex" */
		// alert(childNodes[i].id);
		if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tbcx(\\s|$)'))){	    	
			//childNodes[i].className= 'tbcx';
			//childNodes[i].removeClassName("active");
			childNodes[i].className = removeClass(childNodes[i].className,'active');
			//tab.style.display = 'none'; 
		}
	}//for
	
	cont_tab.style.display='block';
	act_tab.className=act_tab.className+' active';
}//activate_tab


function activate_pricetab(what,cont_id)
{
	var tabber = ge('tabspricecontholder');
	var act_tab = ge(what);
	var cont_tab = ge(cont_id);
	
	if (!document.getElementsByTagName) { return false; }

	  /* Loop through an array of all the child nodes within our tabber element. */
	  childNodes = tabber.childNodes;
	  for(i=0; i < childNodes.length; i++){
	    /* Find the nodes where class="tcont" */
	    if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tcont(\\s|$)'))){
	    	childNodes[i].style.display = 'none'; 
	    }
	  }//for
	  
	  
	  tabber = ge('tbcplst');	  
	  /* Loop through an array of all the child nodes within our tabber element. */
	  childNodes = tabber.childNodes;
		  for(i=0; i < childNodes.length; i++){
		    /* Find the nodes where class="tabex" */
			 // alert(childNodes[i].id);
		    if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tbcx(\\s|$)'))){	    	
		    	 //childNodes[i].className= 'tbcx';
		    	//tab.style.display = 'none';
		    	childNodes[i].className = removeClass(childNodes[i].className,'active');
		    }
		  }//for
		  
	  cont_tab.style.display='block';
	  act_tab.className=act_tab.className+' active';
}//activate_tab

function activate_oftab(what)
{
	var tabber = ge('tbclst');
	var acttab = ge(what);
	  var name;

	  if (!document.getElementsByTagName) { return false; }

	  /* Loop through an array of all the child nodes within our tabber element. */
	  childNodes = tabber.childNodes;
	  for(i=0; i < childNodes.length; i++)
	  {
	    /* Find the nodes where class="tabex" */
		 // alert(childNodes[i].id);
	    if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tbcx(\\s|$)')))
	    {	    	
	    	tab = childNodes[i];
	    	tab.className = '';
	    	//tab.style.display = 'none'; 
	    }
	  }//for
	  
	  //acttab.className='active';
}//hideall_contabs

function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(current,remove) {
var reg = new RegExp('(\\s|^)'+remove+'(\\s|$)');
return current.replace(reg,' ');
}

//function submit form -  sf(form_id)
function sf(fform)
{
	var myForm = document.getElementById(fform);
	try { myForm.submit(); } catch(err) { alert(err);	/* buggi buggi (; */ }
} //sf

function switchLang(lang)
{
	var myAjax = new Ajax.Request('js/ajax/switchlang.php', { method: 'get', parameters: { lang: lang }, onSuccess: function() { window.location.reload(); } });
} //switchLang

function update_tabs_list(pid)
{
	var myAjax = new Ajax.Updater('list_tabs','../js/ajax/list_tabs.php', {method: 'get', parameters: { pid: pid }, onSuccess: function() { update_tabs_display(pid); } });
} //update_tabs_list

function update_gall(id,container)
{
	var myAjax = new Ajax.Updater(container,'../js/ajax/aj_gallery.php', {method: 'get', parameters: { gid: id } });
} //update_gall

function update_files(id,container)
{
	var myAjax = new Ajax.Updater(container,'../js/ajax/aj_files.php', {method: 'post', parameters: { cid: id } });
} //update_files

function reupdateTab()
{
	if(Eupdate)
	{
		update_tab_content(Etab_id,Epid,Etype,Eext_id);
	}
}

function update_tab_content(tab_id,pid,type,ext_id)
{
	Eupdate = true;
	Etab_id = tab_id;
	Epid = pid;
	Etype = type;
	Eext_id = ext_id;

	switch(tab_mode)
	{
		case 'view':
			switch(type)
			{
				case 1: //text
					var myAjax = new Ajax.Updater('tab_content','../js/ajax/view_text.php', {method: 'get', parameters: { id: ext_id }, evalScripts: true, onSuccess: function() { } });
				break;  //1

				case 2: //files
					update_files(ext_id,'tab_content');
				break;  //2

				case 3: //gallery
					update_gall(ext_id,'tab_content');
				break; //2

				case 4: //picture
					var myAjax = new Ajax.Updater('tab_content','../js/ajax/view_pic.php', {method: 'post', parameters: { id: ext_id }, evalScripts: true, onSuccess: function() { } });
				break;  //2

				default:
			}//switch type view
		  break; //case view
		default:

	}//switch tab mode
}//update_tab_content

function fail_validation(el_id)
{
	var failed_el = ge(el_id);
	failed_el.className = failed_el.className + ' error';
}//fail_validation

function tab_click(tab_id,pid,type,ext_id)
{
  var tabber = ge('tabber');
  var name;

  if (!document.getElementsByTagName) { return false; }

  /* Loop through an array of all the child nodes within our tabber element. */
  childNodes = tabber.childNodes;
  for(i=0; i < childNodes.length; i++)
  {

    /* Find the nodes where class="tabex" */
    if(childNodes[i].className && childNodes[i].className.match(new RegExp('(\\s|^)tabex(\\s|$)')))
    {
    	name = childNodes[i];

    	var current_tab_ins = ge('xx'+childNodes[i].id);
    	if(name.id==tab_id) { name.className = 'tabex active'; current_tab_ins.className = 'active'; update_tab_content(tab_id,pid,type,ext_id); } else { name.className = 'tabex'; current_tab_ins.className = ''; }
    }

  }//for
}//tab_click

function acc_dwf(el_id)
{
	var el = ge(el_id);
	el.className = 'download';
}

function dsb_dwf(el_id)
{
	var el = ge(el_id);
	el.className = 'inactive_download';
}

function unsetactivetofs(){
	  var elz = ge('numz');
	  childNodes = elz.childNodes;
	  for(i=0; i < childNodes.length; i++) childNodes[i].className='';
}

function validate_senf(){
	var error= false;
	var offerid = gv('offerid');	
	var destinationid = gv('destinationid');	
	var name = gv('fname');	
	var mail = gv('fmail');	
	var tel = gv('ftel');	
	var hotel = gv('fhotel');	
	var startd = gv('frangesday');	
	var startm = gv('frangesmonth');	
	var endd = gv('frangeeday');	
	var endm = gv('frangeemonth');	
	var people = gv('fpeople');	
	var toemail = gv('toemail');	
	var questions = gv('fquestions');	

	if(name.length<=1) { fail_validation('fname'); error=true; }
	if(mail.length<=1) { fail_validation('fmail'); error=true; }
	if(tel.length<=1) { fail_validation('ftel'); error=true; }
		
	if(error){ return; }
	//return;
	var target = "theform_status";
	var url = '../php/sendrequest.php';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: { destinationid: destinationid, offerid: offerid, toemail: toemail, name: name, mail: mail, phone: tel, hotel: hotel, day1: startd, month1: startm, day2: endd, month2: endm, count: people, questions: questions }, onComplete: function(){
		$('theform').style.display = 'none';
		$(target).style.display = 'block';
		$(target).innerHTML = '<div class="top"><!-- --></div>' + $(target).innerHTML + '<div class="bottom"><!-- --></div>';
		press = true;
	}});
}//validate_senf

function swapdiv(){
	$('theform_status').style.display = 'none';
	$('theform').style.display = 'block';
}

try { document.execCommand('BackgroundImageCache', false, true);} catch(e) {}

function select_list_dest(country,container){
	var myAjax = new Ajax.Updater(container,'../js/ajax/processor.php', {method: 'post', parameters: { action: 'select_list_dest', country: country }});
}//update_hotels_list

function googlesearch(){
	var scountry = gv('scountry');	
	var sdest = gv('sdest');	
	var sprice = gv('sprice');
	var sorder = gv('sorder');
		
	var urltogo = site_url + 'list.php?type=4&c='+scountry+'&d='+sdest+'&pr='+sprice+'&o='+sorder;
	
	document.location.href = urltogo;
}//googlesearch

window.onload = function() { tt_Init(); preloadCssImages.Load(); }
// 
var preloadCssImages = {
	    imgUrls: [],
	    divPreImages: null,
	    
	    Load: function() {
	        this.parseCSS(document.styleSheets);
	    },
	    
	    InitPreImageDiv: function() {
	        this.divPreImages = document.createElement("div");
	        this.divPreImages.style.display = "none";
	        this.divPreImages.style.visibility = "hidden";
	        document.body.appendChild(this.divPreImages);
	    },
	    
	    inArray: function( elem, array ) {
	        for ( var i = 0, length = array.length; i < length; i++ )
	            if ( array[ i ] === elem )
	                return i;
	        return -1;
	    },
	    
	    parseCSS: function(sheets) {
	        var w3cImport =-1;
	    },
	    
	    parseCSS: function(sheets) {
	        var w3cImport = false,
	            imported = [],
	            importedSrc = [],
	            baseURL;
	        var sheetIndex = sheets.length;
	        while(sheetIndex--){//loop through each stylesheet
	            var cssPile = '';//create large string of all css rules in sheet
	            var csshref = (sheets[sheetIndex].href) ? sheets[sheetIndex].href : 'window.location.href';
	            var baseURLarr = csshref.split('/');//split href at / to make array
	            baseURLarr.pop();//remove file path from baseURL array
	            baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
	            if (baseURL) {
	                baseURL += '/'; //tack on a / if needed
	            }
	            if(sheets[sheetIndex].cssRules || sheets[sheetIndex].rules){
	                thisSheetRules = (sheets[sheetIndex].cssRules) ? //->>> http://www.quirksmode.org/dom/w3c_css.html
	                    sheets[sheetIndex].cssRules : //w3
	                    sheets[sheetIndex].rules; //ie
	                var ruleIndex = thisSheetRules.length;
	                while(ruleIndex--){
	                    if(thisSheetRules[ruleIndex].style && thisSheetRules[ruleIndex].style.cssText){
	                        var text = thisSheetRules[ruleIndex].style.cssText;
	                        if(thisSheetRules[ruleIndex].selectorText.indexOf('hover') != -1) { //only add hover class....
	                            if(text.toLowerCase().indexOf('url') != -1){ // only add rules to the string if you can assume, to find an image, speed improvement
	                                cssPile += text; // thisSheetRules[ruleIndex].style.cssText instead of thisSheetRules[ruleIndex].cssText is a huge speed improvement
	                            }
	                        }
	                    } else if(thisSheetRules[ruleIndex].styleSheet) {
	                        imported.push(thisSheetRules[ruleIndex].styleSheet);
	                        w3cImport = true;
	                    }
	                    
	                }
	            }
	            //parse cssPile for image urls
	            var tmpImage = cssPile.match(/[^\("]+\.(gif|jpg|jpeg|png)/g);
	            //reg ex to get a string of between a "(" and a ".filename" / '"' for opera-bugfix
	            if(tmpImage){
	                var i = tmpImage.length;
	                while(i--){ // handle baseUrl here for multiple stylesheets in different folders bug
	                    var imgSrc = (tmpImage[i].charAt(0) == '/' || tmpImage[i].match('://')) ? // protocol-bug fixed
	                        tmpImage[i] :
	                        baseURL + tmpImage[i];
	                    
	                    if(this.inArray(imgSrc, this.imgUrls) == -1){
	                        this.imgUrls.push(imgSrc);
	                    }
	                }
	            }
	            if(!w3cImport && sheets[sheetIndex].imports && sheets[sheetIndex].imports.length) {
	                for(var iImport = 0, importLen = sheets[sheetIndex].imports.length; iImport < importLen; iImport++){
	                    var iHref = sheets[sheetIndex].imports[iImport].href;
	                    iHref = iHref.split('/');
	                    iHref.pop();
	                    iHref = iHref.join('/');
	                    if (iHref) {
	                        iHref += '/'; //tack on a / if needed
	                    }
	                    var iSrc = (iHref.charAt(0) == '/' || iHref.match('://')) ? // protocol-bug fixed
	                        iHref :
	                        baseURL + iHref;
	                    
	                    importedSrc.push(iSrc);
	                    imported.push(sheets[sheetIndex].imports[iImport]);
	                }
	            }
	        }//loop
	        if(imported.length){
	            parseCSS(imported, importedSrc);
	            return false;
	        }
	        if( this.imgUrls && this.imgUrls.length > 0 ) {
	            this.InitPreImageDiv();
	            this.loadImgs();
	        }
	    },
	    
	    loadImgs: function(){
	        if(this.imgUrls && this.imgUrls.length){
	            for ( var i = 0, length = this.imgUrls.length; i < length; i++ )
	            {
	                var img = new Image(); //new img obj
	                img.src = this.imgUrls[i];    //set src either absolute or rel to css dir
	                this.divPreImages.appendChild(img);
	            }
	        }    
	    }
	};


