function OBJ_Search_Symbol()
{
	this.search_symbol_demo_running = false; // positon reader on/off

	this.show_symbol = function(image, x, y, code, competition_id, competition_type, symbol_id)
	{
		document.write('<img src="http://i.tvnet.lv/search_symbol/'+image+'" style="position:absolute; z-index:9000; top:'+y+'px; left:'+x+'px; cursor: pointer;" onclick="');

		if(competition_type==1)
			document.write('SEARCH_SYMBOL.open_register_form(this,\''+code+'\', '+competition_id+');');
		else
			document.write('SEARCH_SYMBOL.show_message(\''+code+'\', '+competition_id+', '+symbol_id+');');

		document.write('" />');
	}

	// symbol addition pannel
	this.show_symbol_panel = function(ss_lang, ss_site, ss_cat)
	{
		// pannel can use only when browser with is 1024px
		window.resizeTo(1024,800);

		// get site and cat
		var url = window.location.href;
		url = url.substr(0,url.length-24);
		var url_exploded = url.split('/');

		// control panel for demo mode
		document.write('<div style="position: fixed; top: 10px; left: 10px; width: 230px; height: 140px; background-color: #eee; border: 1px solid #ddd; z-index: 9000; padding: 3px;">');
		document.write('<h5>Meklējamo Simbolu Panelis</h5>');
		document.write('<label>x: </label><input style="width: 40px;" type="text" name="x" id="x" />');
		document.write('<label> y: </lable><input style="width: 40px;" type="text" name="y" id="y" />');
		document.write('<lable> valoda: </lable><input style="width: 30px;" type="text" name="ss_lang" id="ss_lang" value="'+ss_lang+'" /><br />');
		document.write('<lable>site: </lable><input style="width: 80xp;" type="text" name="ss_site" id="ss_site" value="'+ss_site+'" /><br />');
		document.write('<lable>kategorija: &nbsp;</lable><input style="width: 80xp;" type="text" name="ss_cat" id="ss_cat" value="'+ss_cat+'" /><br />');
		document.write('<input id="search_symbol_demo_starter" type="button" onclick="SEARCH_SYMBOL.search_symbol_demo_running=true;" value="Start" /><br /><em>(skaitītājs apstāsies izdarot dubulklikšķi)</em>');
		document.write('</div>');

		// stop position reader on double click
		document.ondblclick = function(e)
		{
			SEARCH_SYMBOL.search_symbol_demo_running=false;
		}

		// position reader
		document.onmousemove = function(ev)
		{
			if(SEARCH_SYMBOL.search_symbol_demo_running == true)
			{
				ev = ev || window.event;
				if(ev.pageX || ev.pageY)
				{
					document.getElementById('x').value = ev.pageX;
					document.getElementById('y').value = ev.pageY;
				}
				else
				{
					document.getElementById('x').value = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
					document.getElementById('y').value = ev.clientY + document.body.scrollTop  - document.body.clientTop;
				}
			}
		};
	}

	this.show_message = function(code ,competition_id ,symbol_id)
	{
		window.open('http://www.tvnet.lv/ajax/showsearchsymbolinfo/'+code+'/'+competition_id+'/'+symbol_id,'' ,' height=170, width=340 ,left=200 ,top=200, toolbar=no, menubar=no, location=no, status=no, directories=no');
	}

	this.open_register_form = function(obj,code,competition_id)
	{
		//obj.style.display='none';

		var reg_form=window.open('/search_symbol.html','' ,' height=170, width=340 ,left=200 ,top=200, toolbar=no, menubar=no, location=no, status=no, directories=no');
        reg_form.document.write('<scr' + 'ipt type="text/javascript" src="/assets/js/search_symbol.js"></script>');
        reg_form.document.write('<scr' + 'ipt type="text/javascript" src="/assets/js/jquery-1.4.2.min.js"></script>');
        reg_form.document.write('<style type="text/css" > ');
        reg_form.document.write('label { font-weight: bold; } ');
        reg_form.document.write('body { color:#343434; font-family:Verdana,Geneva,Helvetica,Arial,sans-serif; font-size: 12px; } ');
        reg_form.document.write('.text_field { border:0.083em solid #999999; margin-bottom:0.25em; padding:0.25em; -moz-border-radius:4px 4px 4px 4px; -webkit-border-radius:4px 4px 4px 4px; width: 250px; } ');
        reg_form.document.write('.orange_button { -moz-border-radius:4px 4px 4px 4px; -webkit-border-radius:4px 4px 4px 4px; background-color:#FF6600; border:0.0833em solid #FF6600; color:#FFFFFF; padding:0.167em; margin-left: 20px; } ');
        reg_form.document.write('.center_content { text-align: center; } ');
        reg_form.document.write('.red { color: #FF6600; } ');
        reg_form.document.write('a, a:hover { color: #FF6600; } ');
        reg_form.document.write('</style> ');

        reg_form.document.write('<p id="search_symbol_info_text" class="center_content">');
        reg_form.document.write('<b>Ievadi savu e-pasta adresi lai piereģistrētu atrasto SELGAS burtu!</b><br />');
        reg_form.document.write('Sīkāka infromācija par konkursu šeit <a href="http://www.tvnet.lv/selgas_cepumi/" target="_blank">šeit</a>');
        reg_form.document.write('</p>');

        reg_form.document.write('<form method="post" >');
        reg_form.document.write('<input type="hidden" name="code" id="code" value="'+code+'">');
        reg_form.document.write('<input type="hidden" name="competition_id" id="competition_id" value="'+competition_id+'">');

        reg_form.document.write('<div class="bottom_margin center_content" id="e_mail_box">');
        reg_form.document.write('<label for="e_mail">e-pasts:</lable> <input class="text_field" type="text" name="e_mail" id="e_mail" onkeypress="return SEARCH_SYMBOL.blockEnterKey(event);" >');
        reg_form.document.write('</div>');

        reg_form.document.write('<div class="center_content">');
        reg_form.document.write('<input class="orange_button" type="button" onclick="SEARCH_SYMBOL.send();" value="Sūtīt" id="send_button" >');
        reg_form.document.write('<input class="orange_button" type="button" onclick="self.close();" value="Aizvērt" >');
        reg_form.document.write('</div>');

        reg_form.document.write('</form>');

        reg_form.document.close();
	}


	this.send = function()
	{
    	var check = false;

		var info_text = $('#search_symbol_info_text');
 		var code = $('#code').val();
        var e_mail = $('#e_mail').val();
 		var competition_id = $('#competition_id').val();

		// chect wether all fields filled
		if(code.length > 0 && e_mail.length > 0)
			check = true;
		else
			check = false;

		if(check == true)
		{
			// post data and save success message
			$.post( '/ajax/saveSearchSymbol',
			{ code: code, e_mail: e_mail, competition_id: competition_id },
  			function(data){
                if(data.substr(0,7) == 'success')
                {
                    var score = (data.substr(7,((data.length)-7)));

                    if(score == ' S E L G A')
                        info_text.html('<em><b>Jūs esat atradis visus burtus!</b><br /><br />Paldies par piedalīšanos konkursā.<br />Sīkāka infromācija par konkursu šeit <a href="http://www.tvnet.lv/selgas_cepumi/" target="_blank">šeit</a></em>');
                    else
                        info_text.html('<em>Veiksmīgi piereģistrēts burts!</em><br />Jūs esat atradis: <em class="red"><b>' + (data.substr(7,((data.length)-7))) + '</b></em>');

                    $('#e_mail_box').hide();
                    $('#send_button').hide();
                }
                else
                {
                    var score = (data.substr(5,((data.length)-5)));

                    if(score == ' S E L G A')
                        info_text.html('<em><b>Jūs esat atradis visus burtus!</b><br /><br />Paldies par piedalīšanos konkursā.<br />Sīkāka infromācija par konkursu šeit <a href="http://www.tvnet.lv/selgas_cepumi/" target="_blank">šeit</a></em>');
                    else
                        info_text.html('<em class="red"><b>Kļūda!</b> Šo burtu esat jau iepriekš piereģistrējis!</em>' + '<br />Jūs esat atradis: <em class="red"><b> ' + (data.substr(5,((data.length)-5))) + '</b></em><br />');

                    $('#e_mail_box').hide();
                    $('#send_button').hide();
                }
            },
  			"html");
		}
		else
		{
			// show error message
			info_text.html('<em class="red"><b>Kļūda!</b> Pārbaudiet vai e-pasts ievadīts korekti!</em>');
		}

	}

    this.blockEnterKey = function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        var result = true;

        if (keyCode == 13) result = false;

        return result;
    }

}

var SEARCH_SYMBOL = new OBJ_Search_Symbol();


