﻿/*global self, window, $, $find, radopen */

var hasPopped=0;
var MPEID = "";
var inputDelayer=delayTimer(1000);
$(document).ready(function() {
   //Sys.Application.add_load(modalSetup);    
  applyMagnifier();
});
function settimer(){
     inputDelayer(function(){
          swapLPImage();
     });
}
function setupInitialMagnifier()
{
    inputDelayer(function(){
        clearTimeout(inputDelayer());
        inputDelayer();
        setFieldScripts();
         $('a.zoom').attr('href',$('a.zoom').attr('href').replace("size[300]", "size[750]"));
        $('a.zoom').magnify({lensWidth: 200,lensHeight: 200,link: false});
     });
}
function modalSetup() 
{
    var modalPopup = $find(MPEID);
    modalPopup.add_shown(onModalShown); 
}
          
function setFieldScripts()
{
    $(".clearField[value^='Enter']").clearField();
    $('.numeric').format({precision: 0,allow_negative:false,autofix:true});
    $('.date').dateEntry({ spinnerImage: '' });
    $('.greekfield').keypad({keypadOnly: true, separator: '|', layout: ['&#913;|&#914;|&#915;|&#916;|&#917;|&#918;', '&#919;|&#920;|&#921;|&#922;|&#923;|&#924;', '&#925;|&#926;|&#927;|&#928;|&#929;|&#931;', '&#932;|&#933;|&#934;|&#935;|&#936;|&#937;',$.keypad.CLOSE], onKeypress: function(key, value, inst) { this.value = limitGreekInput(value.replace("Enter Greek Name", ""), $(this).attr("maxlength")); } });

    
}
function swapLPImage(imageID,newSrc,linkID)
{
    clearTimeout(inputDelayer());
    inputDelayer();
    $('.photoloader').remove();
    $('#customPhotoHolder').append('<div class="photoloader"><img src="/Images/loaderb64.gif" /></div>');
    $('<img id=\"customPhoto\" />').attr('src', getlpString()+'&t='+new Date().getTime()).load(
        function(){
            $('.customPhoto').attr('src', $(this).attr('src'));
            $('.photoloader').remove();
        }
    );
    setFieldScripts();
    $('a.zoom').attr('href',(getlpString()+'&t='+new Date().getTime()).replace("size[300]", "size[750]"));
    $('a.zoom').magnify({lensWidth: 350,lensHeight: 350,link: false});
}
function swapThumb(newSrc)
{
    $('.mainPhoto').attr('src',newSrc);
    $('.mainzoom').attr('href',newSrc.replace('300/300','750/750'));
    applyMagnifier();
}
function onModalShown()
{   

    setFieldScripts();
    if(hasPopped==0)
    {
        //$('.stoneList').sSelect();
        //$('.optList').sSelect();
        hasPopped=1;
    }
}
function ToUpper(ctrl) 
{
    var t = ctrl.value;
    ctrl.value = t.toUpperCase(); 
}
function limitLength(ctrl, e, maxLength) {
    if(!checkSpecialKeys(e))
    {
        if(ctrl.value.length == maxLength / 100)
        {
            if(window.event)//IE
                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
}
function limitGreekInput(value,maxLength)
{
    return value.substring(0, maxLength / 100);
}
function checkSpecialKeys(e)
{
    if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
        return false;
    else
        return true;
} 
function updateView(sender, eventArgs) {
    
}
function romanize(num) {
  var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},roman = '',i;
  for ( i in lookup ) {
    while ( num >= lookup[i] ) {
      roman += i;
      num -= lookup[i];
    }
  }
  return roman;
}
function RomanDate(date)
{
    var splitDate = date.split("/");
    return romanize(splitDate[0]) + " " + romanize(splitDate[1]) + " " + romanize(splitDate[2]);
}
function fixDate(date)
{
    var splitDate = date.split("/");
    return parseInt(splitDate[0])+"/"+parseInt(splitDate[1])+"/"+splitDate[2];
}
function delayTimer(delay){
     var timer;
     return function(fn){
          timer=clearTimeout(timer);
          if(fn)
               timer=setTimeout(function(){
               fn();
               },delay);
          return timer;
     }
}
function applyMagnifier()
{

	$('a[rel*=magnify]').magnify({
	    lensWidth: 200, // width of the lens
		lensHeight: 200, // height of the lens
		link: false // clicking in the lens goes to the large image
	});
    
}


