var basehref =  "http://www.autopanel.pl/";

var middlethumbnail = "_th";

var imagesdir = "./templates/new/images/";

var template = "new";

var thumbnail = "_sm";

var middlethumbnail = "_th";


function Goto(href) {
 window.location.href = href;
}

function Back() {
 history.back(-1);
}



function Back2Offers() {
 window.location.href = "./?m=search&typ=0";
}


function Sort(indx) {

path = window.location.href;

pos = path.indexOf("&o=");

if (pos == -1) { 
path = path + "&o="+indx; 
} else {
path = path.substr(0,pos);
path = path + "&o="+indx; 
}

Goto(path);

}


function IsValidObject(objToTest)
{
if (objToTest == null || objToTest == undefined) {
return false;
}
return true;
}

function ShowWindow(address,width,height)
{
 window.open(address, "", "width="+width+", height="+height+", scrollbars, resizable");
}

function PrintOffer(guid) {
 ShowWindow(basehref+"/?m=print&s=offer&guid="+guid,800,600);
}

function ShowRegulations() {
 ShowWindow(basehref+"regulamin.pdf");
}


function PrintPage() {
 window.print();
}

function SubmitForm(formname)
{
 var form = document.getElementById(formname);
 if (IsValidObject(form)) { form.submit(); }
}

function addOption(selectId, txt, val) {
    var objOption = new Option(txt, val);
    document.getElementById(selectId).options.add(objOption);
}

function selOption(selectId,val) {

document.getElementById(selectId).selectedIndex = val;
}

function clearOptions(selectId) {
 
 var lb = document.getElementById(selectId);
 
 for (var i=lb.options.length-1; i>=0; i--){
     lb.options[i] = null;
  }
  lb.selectedIndex = -1;
}

function checkEnter(e){ 
var characterCode;

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
return false
}
else{
return true
}
}

function LoginEnter(e,val){ 
var characterCode;

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
xajax_Login(val);
}
else{
return true
}
}

function HideInfo() {

if ($('#secInfo').is(":visible"))  { $('#secInfo').hide(); }
}

function DisplayVersion(show) {

if (show) { $('#secVersion').show(); } else { $('#secVersion').hide(); }

}

function ScrollUp(divid)
{
 $(window).scrollTo( $("#"+divid), 800 );
}

function PageUp() {

 window.scroll(0,0);

}

function DeleteClients() {

jConfirm('Czy jesteś pewien, że chcesz usunąć klienta?', 'Potwierdzenie', function(r) {  
if( r ) xajax_DeleteClients(xajax.getFormValues('dealersFrm'));
});


}

function SlideLoginInfo() {
    $('#loginInfoText').css({"left": "-150px"}).animate({"left": "370px"}, 500,  "easeOutBounce").delay(2000).animate({"left": "870px"}, 500, "easeInBounce");
}


function ShowInfo(itype) {

  if ($('#secInfo').is(":hidden"))  
  { 
	if (itype=="0") { $('#secInfoText').attr('style','color: #000000;'); } else {	$('#secInfoText').attr('style','color: red;'); }
	$('#secInfo').show(); 
	PageUp();
	}
  
}

function SetInfo(text) {

 $('#secInfoText').attr('innerHTML',text); 
  
}

function HideInfo() {

if ($('#secInfo').is(":visible"))  { $('#secInfo').hide(); }
}

function ScrollUp(divid)
{
 $(window).scrollTo( $("#"+divid), 800 );
}

function PageUp() {

 window.scroll(0,0);

}

function GetOIDS() {

check = document.getElementById('off0');
i = 0;
oids = "";

 do
  {
	 if (check.checked==true) {
	 nameh = "h"+i;
   hid = document.getElementById(nameh); 
	 if (oids=="") { oids = "'"+hid.value+"'"; } else { oids = oids + ",'"+hid.value+"'"; }
	 }
	 i = i + 1;
	 name = "off"+i;
	 check = document.getElementById(name);
	 }
	 while (IsValidObject(check));
	
 return oids;	
}



function ExportOffers() {
 GetOIDS();
 if (oids!="") { 
jConfirm('Czy jesteś pewien, że chcesz eksportować ogłoszenia?', 'Potwierdzenie', function(r) {  
if ( r ) xajax_ExportStart(oids); 
});
}
}

function DeleteOffers() {
 oids = GetOIDS();
 if (oids!="") { 
jConfirm('Czy jesteś pewien, że chcesz usunąć ogłoszenia?', 'Potwierdzenie', function(r) {  
if( r ) xajax_DeleteOffers(oids);
});
}

}

function ImportOtomoto() {
jConfirm('<b>Uwaga!</b> Kontynuacja spowoduje <u>skasowanie</u> wszystkich ogłoszen z serwisu AutoPanel oraz pobranie aktualnych ofert z OtoMoto!\n Czy jesteś pewien, że chcesz importować ogłoszenia?', 'Potwierdzenie', function(r) {  
if ( r ) window.location.href="./?m=import"; 
});
}

function Export() {
jConfirm('Czy jesteś pewien, że chcesz eksportować ogłoszenia?', 'Potwierdzenie', function(r) {  
if ( r ) window.location.href="./?m=export2"; 
});
}

function GoExport(oids,dids) {

document.getElementById('progressreport').value='';

 l = dids.length;
 
 for (i=0;i<l;i++) {

 did = dids[i];

 did = did.replace("'","");
 did = did.replace("'","");
 
 xajax_DeleteOffer(did); 
 
 }
 
 l = oids.length;
 
 lp = 0;
 
 soids = oids.join(",");
 soids = soids.toString();

 xajax_ExportKupautoOffers(soids); 
 
 for (i=0;i<l;i++) {

 oid = oids[i];
 oid = oid.replace("'","");
 oid = oid.replace("'","");
 
 lp = i + 1;
 
 xajax_ExportOffer(oid,lp,l); 
 
 }
}

function GoImport(oids) {

document.getElementById('progressreport').value='';

  
 l = oids.length;
 
 lp = 0;
 
 for (i=0;i<l;i++) {

 oid = oids[i];
 oid = oid.replace("'","");
 oid = oid.replace("'","");
 
 lp = i + 1;
 
 xajax_ImportOffer(oid,lp,l); 
 
 }
}


function ExportProgressInfo(infotext) {
 
 $('span[id="progresstext"]').attr('innerHTML',infotext);

 }

function ExportReportInfo(infotext) {

ctext = document.getElementById('progressreport').value;

document.getElementById('progressreport').value = infotext +"\r"+ctext;

}

function ExportEndInfo(infotext) {

 $('#infotext').attr('innerHTML',infotext);
 $('td[id="pleasewait"]').hide();

}

function SetFull1(state) {

document.getElementById('pelna_opcja_bez_skory').checked = false;

document.getElementById('abs').checked = state;
document.getElementById('alarm').checked = state;
document.getElementById('alufelgi').checked = state;
document.getElementById('centralny').checked = state;
document.getElementById('el_szyby').checked = state;
document.getElementById('el_lusterka').checked = state;
document.getElementById('immobiliser').checked = state;
document.getElementById('klimatyzacja').checked = state;
document.getElementById('poduszka').checked = state;
document.getElementById('radiocd').checked = state;
document.getElementById('skora').checked = state;
document.getElementById('welur').checked = false;
document.getElementById('wspomaganie').checked = state;
ToggleAC(state);
}

function SetFull2(state) {

document.getElementById('pelna_opcja').checked = false;

document.getElementById('abs').checked = state;
document.getElementById('alarm').checked = state;
document.getElementById('alufelgi').checked = false;
document.getElementById('centralny').checked = state;
document.getElementById('el_szyby').checked = state;
document.getElementById('el_lusterka').checked = state;
document.getElementById('immobiliser').checked = state;
document.getElementById('klimatyzacja').checked = state;
document.getElementById('poduszka').checked = state;
document.getElementById('radiocd').checked = state;
document.getElementById('skora').checked = false;
document.getElementById('welur').checked = state;
document.getElementById('wspomaganie').checked = state;
ToggleAC(state);
}

function ToggleEnVKV(state) {
    if (state) $('#EnVKV').show();
    else $('#EnVKV').hide();
}

function ToggleAC(state) {
    if (state) $('#rodzaj_klimy').show();
    else $('#rodzaj_klimy').hide();
}

function CheckAll(state,indx)
{

if (indx==1) { document.getElementById('offall2').checked = state; }
if (indx==2) { document.getElementById('offall1').checked = state; }

check = document.getElementById('off0');
i = 0;

 do
  {
	 check.checked = state; 
	 i = i + 1;
	 name = "off"+i;
	 check = document.getElementById(name);
	 }
  while (IsValidObject(check))	
}

function CheckAllD(state,indx)
{
if (indx==1) { document.getElementById('deaall2').checked = state; }
if (indx==2) { document.getElementById('deaall1').checked = state; }

check = document.getElementById('dea0');
i = 0;

  do
  {

	 check.checked = state; 
	 i = i + 1;
	 name = "dea"+i;
	 check = document.getElementById(name);
	 }
	while (IsValidObject(check))
}


function ShowImage(imgname) {
 $('img[id="galPrv"]').hide();
 $('img[id="galPrv"]').attr('src',imgname);
 $('img[id="galPrv"]').fadeIn();
}

function ShowImages() {
 window.open("templates/"+template+"/showimage.php", '','menubar=0, toolbar=0, location=0, scrollbars=0, resizable=0, status=0,width=640, height=600');
}

function LoadImage(imgname,imginx) {
 if ((imgname!="") && (imginx==-1)) {
 imgname = imgname.replace(thumbnail,"");
 imgname = imgname.replace(middlethumbnail,"");
 }
 if ((imgname=="") && (imginx!=-1)) {
 imgname = document.getElementById('imgTh'+imginx).src;
 imgname = imgname.replace(thumbnail,"");
 }

 $('img[id="imgPrv"]').hide();
 $('img[id="imgPrv"]').attr('src',imgname);
 $('img[id="imgPrv"]').fadeIn();
}


function LoadImages() {
 oh = window.opener.document;
 LoadOtherImages(); 
 LoadImage(oh.getElementById('galPrv').src,-1);
 }
 
function LoadOtherImages() {
 oh = window.opener.document;

 HTML = "<table class='table-thumbnail' cellpadding='0' cellspacing='0' align='center'><tr>";
	for (t=1; t<=10; t++)
  {
   var img = oh.getElementById('galTh'+t);
	 //alert(img.src);
	 if (img!=null) { 
	 func = "javascript:LoadImage("+"\"\""+","+t+")";
	 HTML = HTML + "<td class='td-thumbnail'><a href='"+func+"'><img class='imgvTh' id='imgTh"+t+"' src='"+img.src+"' alt=''></a></td>";
	 }
  }	
 HTML = HTML + "</tr></table>"; 
 document.getElementById('otherphotos').innerHTML = HTML;
} 

$().ready(function() {

if (IsValidObject(document.getElementById('eoid'))) {
aeoid = new Array();
woid = document.getElementById('eoid').value;
aeoid = woid.split(",");
}
if (IsValidObject(document.getElementById('doid'))) {
adoid = new Array();
woid = document.getElementById('doid').value;
adoid = woid.split(",");
}
if (IsValidObject(document.getElementById('ioid'))) {
idoid = new Array();
woid = document.getElementById('ioid').value;
idoid = woid.split(",");
}


var myOpen=function(hash){ hash.w.css('opacity',0.88).show(); GoExport(aeoid,adoid); };
$('#dialog').jqm({onShow:myOpen}); 
var myClose=function(hash) { x = 0; };
$('#dialog').jqm({onHide:myClose}); 

var myOpeni=function(hash){ hash.w.css('opacity',0.88).show(); GoImport(idoid); };
$('#dialogi').jqm({onShow:myOpeni}); 
var myClosei=function(hash) { x = 0; };
$('#dialogi').jqm({onHide:myClosei}); 

var myOpena=function(hash){ hash.w.css('opacity',0.88).show(); };
$('#dialoga').jqm({onShow:myOpena}); 
var myClosea=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };
$('#dialoga').jqm({onHide:myClosea});

$('#tabs').tabs({ fx: { height: 'show', opacity: 'show' }, 
    select: function(event, ui) {
        valanalyzefrm();      
        $("#analyse").val(ui.index);
        switch (ui.index) {
            case 0:
                $('.td-dist').show();
                $('.td-vehicle').show();
                break;;
            case 1: 
                $('#GoBtn').show();
            case 1: case 2: case 3:
                $('.td-dist').hide();
                $('.td-vehicle').show();
                break;;
            case 4:
                $('.td-dist').show();
                $('.td-vehicle').hide();
                $('#GoBtn').show();
                break;;
        }
        if ($('#AnalyseRes').html().length > 10) {
            $('#AnalyseRes').html('');
        }
    }
});


});

function AjaxLoad() {
    $('#AnalyseRes').html('<div style="text-align: center; margin: 10px;"><img style="border-left: 1px #777 dotted;border-right: 1px #777 dotted;" src="templates/default/images/car.gif" alt="Loading..." /></div>');
}

$(document).ready(function(){

  $( "input:submit, a, input", ".start" ).button();



$("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag

	$("#topnav li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the <a> tag
		$(this).find("span").show().html(linkText); //Add the text in the <span> tag
	}); 

	$("#topnav li").hover(function() {	//On hover...
		$(this).find("span").stop().animate({
			marginTop: "-40" //Find the <span> tag and move it up 40 pixels
		}, 250);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0"  //Move the <span> back to its original state (0px)
		}, 500, "easeOutBounce");
	});


 var new_map='<span class="map"><span class="s1" /><span class="s2" /><span class="s3" /><span class="s4" /><span class="s5" /><span class="s6" /><span class="s7" /><span class="s8" /><span class="s9" /><span class="s10" /><span class="s11" /><span class="s12" /><span class="s13" /></span>';
 var new_bg=$("<span>");
 new_bg.addClass("bg");
 $("#polska li a").append(new_map);
 $("#polska li a").append(new_bg);

   $('tr[id*="offer_"]').mousemove(function() {
	 $(this).addClass('tr-offer-highlighted');
	 });
	 
	 $('tr[id*="offer_"]').mouseout(function() {
	 $(this).removeClass('tr-offer-highlighted');
	 });
	 
	 $('tr[id*="offer_"]').click(function() {
	 oid = $(this).attr('id').replace("offer_", "");
	 window.location.href = "./?m=actiono&id="+oid;
	 });
	 
	 $('#end').click(function() {
	 $('#dialog').jqmHide(); 
	 });
	 
	 $('#endi').click(function() {
	 $('#dialogi').jqmHide(); 
	 });
	 
	 $('#sela').click(function() {
	 document.getElementById('allegro_id').value = document.getElementById('allid').value;
	 xajax_ShowAllegroCategoryName(document.getElementById('allegro_id').value);
	 $('#dialoga').jqmHide(); 
	 }); 	 
	 
	 $('#enda').click(function() {
	 $('#dialoga').jqmHide(); 
	 }); 
	 
	 $('tr[id*="dealer_"]').mousemove(function() {
	 $(this).addClass('tr-dealer-highlighted');
	 });
	 
	 $('tr[id*="dealer_"]').mouseout(function() {
	 $(this).removeClass('tr-dealer-highlighted');
	 });
	 
	 $('tr[id*="dealer_"]').click(function() {
	 oid = $(this).attr('id').replace("dealer_", "");
	 window.location.href = "./?m=actiona&id="+oid;
	 });
	 
});


function GoToPage($page,$sort) {
     $('#AnalyseRes').hide('drop');
     xajax_GoAnalyse(xajax.getFormValues('analyseFrm'),--$page,$sort); 
}

function ShowTemplate(id) {
 window.open("./?m=template&id="+id, "", "width=1024, height=800, scrollbars, resizable");
}

function chkanalyzefrm() {
if ( ($("select[name='model'] option:selected").val() > 0) && ($("select[name='marka'] option:selected").val() > 0) && ($("select[name='rocznik'] option:selected").val() > 0)) {
    return true;
   }
else { 
    return false;
    }
}

function valanalyzefrm() {
 if (chkanalyzefrm()) {
   $('#GoBtn').show('explode');
 } else {
    $('#GoBtn').hide();
}

}

