var currentLink = document.URL;


$(document).ready(
	function(){
		zipAddEvents();
	}
);

function zipAddEvents(){  

	$('#ad_view a.show').click(adShow);
	$('#popup_ct a.zip_child, #view_menu a.zip_child, #popup_ct a.dr').click(
		function(){
			if(window.opener.location != 'http://www.zip.lv/export/kasjauns.php'){
				window.opener.location = this.href;
				window.opener.focus();
				return false;
			}
		}
	);
	$('#ad_view a.zip_ajax, #ad_view .navigation a').click(
		function(){
			currentLink = this.href;
			if(true ||  this.href.indexOf('view')>-1){
				url = this.href.split('#')[0];
				divider = url.indexOf('?')>-1?'&':'?';
				$('#ad_view').load( url + divider+'ajax=1',null,zipAddEvents);
			}
			if(this.href.indexOf('#zip_center')>-1){
				window.location.href='#zip_center';
			}
			return false;
		}
	);
	// +++++++++++++++++++++++++ FAVORITE ICON +++++++++++++++++++++++++++++++++++	
  var fav_icon_on = 'http://ifrype.com/img/zip/star_on.png';
  var fav_icon_off = 'http://ifrype.com/img/zip/star_off.png';
  function fav_icon_onclick( e ){
    if( typeof( e ) != 'undefined' && e.stopPropagation ) {    
      e.stopPropagation();
    } else if( window.event ) {    
      window.event.cancelBubble = true;
    } // if( event.stopPropagation ) {
    if( this.isfav ){
      //this.src = fav_icon_off;
      this.className = 'fav_icon fav_icon_off';
      this.title_ = fav_icon_title_on;
      $.get( '/'+ZIP + 'show/?exit&fav=0&i=' + this.ad_id );
    } else {
      //this.src = fav_icon_on;
      this.className = 'fav_icon fav_icon_on';
      this.title_ = fav_icon_title_off
      $.get( '/'+ZIP + 'show/?exit&fav=1&i=' + this.ad_id );
    }
    $('#tooltip h3').html(this.title_);    
    this.isfav = ! this.isfav;
    return false;
  }  
  $('#ad_view a.fav').each(
		function(){
      var ad_id = this.getAttribute('adid');		  
		  if( Number( this.getAttribute('isfav') ) ){
  		  this.fav_icon = DOM.make( {
          tag:'div',
          className:'fav_icon fav_icon_on',
          prop:{
            ad_id:ad_id,
            isfav:true,
            src:fav_icon_on,
            onclick:fav_icon_onclick,
            title_:fav_icon_title_off,
            title:' '
          }
        } ).Append( this );
        $(this.fav_icon).tooltip({delay: 0,track: true, showURL: false});
      }
			this.onmouseover=function(){			  
				if( ! this.fav_icon ){
          this.fav_icon = DOM.make( {
            tag:'div',
            className:'fav_icon fav_icon_off',
            prop:{
              ad_id:ad_id,
              isfav:false,              
              src:fav_icon_off,
              onclick:fav_icon_onclick,              
              title_:fav_icon_title_on,
              title:' '              
            }
          } ).Append( this );          
         $(this.fav_icon).tooltip({delay: 0,track: true, showURL: false});
        } else{
          this.fav_icon.style.visibility = '';
        }
        $('#tooltip h3').html(this.fav_icon.title_);                
			}; // // this.onmouseover=function(){
			this.onmouseout=function(){
        if( this.fav_icon && ! this.fav_icon.isfav ) this.fav_icon.style.visibility = 'hidden';			 
			};
		}
	);
	// ------------------------- FAVORITE ICON -----------------------------------
	$('#choose_category a.zip_create').click(
		function(){
			url = this.href.split('#')[0];
			divider = url.indexOf('?')>-1?'&':'?';
			$('#zip_center').load( url + divider+'ajax=1',null,zipAddEvents);
			return false;
		}
	);
	$('#ad_view_admin a.zip_delete').click(
			function(){
				if(confirm(confirmDeleteMsg)){
					return true;
				}
				return false;
		}
	);

	$('#currency_select').each(
		function(){
			this.onchange=function(){
				url = document.URL.split('#')[0];
				divider = url.indexOf('?')>-1?'&':'?';
				currency = this.options[this.selectedIndex].value;
				if(currentLink.indexOf('search')>-1){
					$('#ad_view').load( url + divider+'ajax=1&currency='+currency,null,zipAddEvents);
				} else {
					window.location = url + divider+'currency=' + currency;
				}
				return false;
			}
		}
	);	
	$('#navi_list li a').click(navi_list);

	
	//console.info("ZIP Events loaded");
}




function navi_list(){
	child = $(this).parent('li').children('ul');
	if(child.size()){
		$('#navi_list ul ul').not($(this).parents()).slideUp(500);
		if(child.css('display')=='none'){
			child.slideDown(500);
		} else {
			child.slideUp(500);
		}	
		return false;
	}
}


function adShow(){
	url = this.href;
	place = this.getAttribute('rel');
	if(typeof(search) != 'undefined' && place){
		url +='&search='+search+'&place='+place;
	}
	return adPopup(url);
}
	
function adPopup(url){
	var w=730;
	var h=620;
	var left = (screen.width) ? (screen.width-w)/2 : 0;
	var top = (screen.height) ? (screen.height-h)/2 : 0;
	var handle;
		
	if(handle=window.open(url,'ad_view'+(new Date().getTime()) ,'width='+w+', height='+h+', left='+left+', top='+top+', scrollbars=yes')){
		handle.focus();
	}
		return false;
}




function showImg(id){
	var obj;	
	if(obj = $('#adimg_'+id).get(0)){
	 var rightlink = '&nbsp;';
	 var leftlink = '&nbsp;';
	
	 InfoLite.open({text:'<img id="img_preview" alt="" src="http://ifrype.com/img/zip/load.gif" onclick="showImg('+(id+1)+')" />', width:32, height:32});
		var pre = new Image();
		pre.onload = function(){

			if(id>0){
				leftlink = '<a id="previmg"	onclick="showImg('+(id-1)+'); return false;" href="#"><img src="http://ifrype.com/img/px.gif" alt="" /></a>';
			} else {
				leftlink = '';
			}
			if($('#adimg_'+(id+1)).size()){
				rightlink = '<a id="nextimg" onclick="showImg('+(id+1)+'); return false;" href="#"><img src="http://ifrype.com/img/px.gif" alt="" /></a>';
			}
			
			InfoLite.open({text:'<table width="100%" height="100%"><tr><td align="center"><div id="img_holder">'+leftlink+'<img id="img_preview" alt="" src="" onclick ="InfoLite.close();return false;" />'+rightlink+'</div></td></tr></table>', width:pre.width, height:pre.height+15});
			$('#img_preview').attr('src','');
			$('#img_preview').attr('src',obj.href);
			if($('#adimg_'+(id+1)).size()){
				var pre2 = new Image();

				pre2.src = $('#adimg_'+(id+1)).get(0).href;
			}
			$('#previmg, #nextimg').css('height',pre.height+'px');
			
		}
		pre.src = obj.href;
		
	} else {
		try {
			InfoLite.close();
		}
		catch(e) {}
	}
}

function listShowHide(list,textbox){
	if($(list).val()=='0' || list.options.length<=2){
		$('#'+textbox).show();
	} else {
		$('#'+textbox).hide();
		$('#'+textbox).val('');
	}
}

function loadList(select, title, listname, listid, other, hide_if_empty, show_if_empty, cat, type){
	if(listid!=null){
		listname+='&p='+listid;
		if(cat!=null){
			listname+='&c='+cat;
			if(type!=null){
				listname+='&t='+type;	
			}	
		}
	}
	
	select = $('#'+select).get(0);
	var old = select.options[0].value;
	if(old==''){
		oldtext = select.options[0].text;
	}
	
	$.getJSON('/'+ZIP+'rq/list.php?l='+listname+(other?'&d':''),
        function(data){
        	for(i=select.options.length-1;i>=0;i--){
				select.remove(i);
			}
			if(old==''){
				select.options[select.options.length] = new Option(oldtext, '');
        	}
        	for (var i in data.list ){
				var s = new Option(data.list[i]['title'], i);
				if(data.list[i]['x'] && data.list[i]['y']){
					s.setAttribute('x',data.list[i]['x']);
					s.setAttribute('y',data.list[i]['y']);
				}
				if(data.list[i]['z']){
					s.setAttribute('z',data.list[i]['z']);
				}
				select.options[select.options.length] = s;
			} 
        	if(title){
        		$('#'+title).html(data['title']);
        	}
        	
 			if(hide_if_empty!=null){
				if(select.options.length>(other?2:1)){
					$(hide_if_empty).show();
				} else {
					$(hide_if_empty).hide();
				}			
			}
			if(show_if_empty!=null){
				if(select.options.length>(other?2:1)){
					$(show_if_empty).hide();
				} else {
					$(show_if_empty).show();
				}			
			}
        });
}

function izcel_paraugs( e, r, s ){
  var w = 470, h = 550;
  InfoLite.open({text:'<table><tr><td>'+
  '<div style="position:relative;"><img id="img_preview" alt="" src="'+e.href+'" />' +
  '<div class="paraugs_t1">'+ r + '</div>' +
  '<div class="paraugs_t2">'+ s + '</div>' +
  '</div></td></tr></table>', width:w, height:h+50});

  return false;
}

function noteikumi( e ){
  InfoBox.iframe( e.href, { title:e.title, width:500, height:400, cache:false } );
  return false;
}

function paid_option(href, title){
	InfoBox.open(href+'&ajax=1', { title:title, width:480, height:350, cache:false } );
	return false;
}


function block( e ){
  InfoBox.iframe( e.href, { title:e.title, width:320, height:220, cache:false } );
  return false;
};

function link_iframe( e ){
  InfoBox.iframe( e.href, { title:e.title, width:Number(e.getAttribute('iw')), height:Number(e.getAttribute('ih')), cache:false } );
  return false;
};

function sel_color_radio( e ){
  document.getElementById( 'ad_color_' + e.getAttribute( 'ad_color' ) ).checked = true;
}

function change_list_order( e ){
  var e2 = e.options[ e.selectedIndex ];
 $('#ad_view').load(e.getAttribute( 'url' ) + '?sort=' + e2.getAttribute( 'sort' ) + '&order=' + e2.getAttribute( 'order' )+'&ajax=1',null,zipAddEvents);
}

function fx_blink( e, par ){  
  par.count;  
  if( ! par.time ) par.time = 150; 
  if( e.timeout ) clearTimeout( e.timeout );
  if( par.count < 1 ) return false;  
  e.style.backgroundColor = '#0095AD';
  e.style.color = '#fff';
  e.timeout = setTimeout( function(){
    e.style.backgroundColor = '';
    e.style.color = '';
    par.count --;
    e.timeout = setTimeout( function(){
      fx_blink( e, par );
    }, par.time ); 
  }, par.time );  
}



function map_position( select ){
  var e = select.options[ select.selectedIndex ];
  if( typeof( map ) != 'undefined' && Number( e.getAttribute( 'x' ) ) && Number( e.getAttribute( 'y' ) ) ){
    if( Number( e.getAttribute( 'z' ) ) ) map._.setCenter( new GLatLng( Number( e.getAttribute( 'y' ) ), Number( e.getAttribute( 'x' ) ) ) );
    if( Number( e.getAttribute( 'z' ) ) ) map._.setZoom( Number( e.getAttribute( 'z' ) ) );      
  }
  showMap(document.getElementById('show_map_link'), true);  
}

function showMap(e, a){
  var div = document.getElementById( 'map_canvas_' );  
  if( Number( div.offsetHeight ) < 300 || ( typeof( a ) != 'undefined' && a ) ){
    div.style.height = 'auto';
    div.style.visibility = '';
    e.innerHTML = hide_map_text;
  } else {
    div.style.height = '1px';
    div.style.visibility = 'hidden';
    e.innerHTML = show_map_text;
  }
  return false;
}

function default_img(e){
  document.location.href+='&default=' + e.value;
}

var xZip = {};
function dic( text ){
  if( ! text ) return '';  
  if( xZip[ text ] ) return xZip[ text ]; else return '"' + text + '"';
}

