function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i < a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}



$(document).ready(function(){
    
   
   fnResizeStoreList();
   fnNewsCust();
   fnShowRibbon();
});
function fnShowRibbon()
{
  
    $("#ribbon").fadeIn(2000);
}
function fnInitSlider()
{
     $('#loopedSlider').loopedSlider({
    	autoStart: 8000,
    	restart: 5000,
        slidespeed: 3000
   }); 
}

function fixBlogList(isIn) {

    var left = $("div#blogs-left-column");
    var right = $("div#blogs-right-column");
    var leftItems = $(left).children();
    var rightItems = $(right).children();

    if(!isIn) {
    
        for(var i=0; i<leftItems.length; i++) {
            if($(rightItems[i]).height()>$(leftItems[i]).height()) {
                //alert("Height of Left Column Blog Item was Changes from "+$(leftItems[i]).height()+"px to "+$(rightItems[i]).height()+" at Index :: "+i);
                $(leftItems[i]).css({"height":$(rightItems[i]).height()+"px"});
            } else {
                //alert("Height of Right Column Blog Item was Changes from "+$(rightItems[i]).height()+"px to "+$(leftItems[i]).height()+" at Index :: "+i);
                $(rightItems[i]).css({"height":$(leftItems[i]).height()+"px"});
            }
        }
    } else {
    
        for(var i=0; i<leftItems.length; i++) {
            if($(rightItems[i]).height()>$(leftItems[i]).height()) {
                //alert("Height of Left Column Blog Item was Changes from "+$(leftItems[i]).find("div.blog-item").height()+"px to "+$(rightItems[i]).find("div.blog-item").height()+" at Index :: "+i);
                $(leftItems[i]).find("div.blog-item").css({"height":$(rightItems[i]).find("div.blog-item").height()+"px"});
            } else {
                //alert("Height of Right Column Blog Item was Changes from "+$(rightItems[i]).find("div.blog-item").height()+"px to "+$(leftItems[i]).find("div.blog-item").height()+" at Index :: "+i);
                $(rightItems[i]).find("div.blog-item").css({"height":$(leftItems[i]).find("div.blog-item").height()+"px"});
            }
        }
    }

    //setTimeout('fixBlogList(isIn)', 1000);
}

function fnInitSliderCMSActive()
{
     $('#loopedSlider').loopedSlider({
    	autoStart: 0,
        containerClick:false,
    	restart: 5000,
        slidespeed: 3000,
        extPagination:".extpagination"
       
       
    }); 
}
$(document).ready(function() {
	try{ initCms(); } catch(e){ alert("Error:"+e);}
});

$(window).resize(function(){
	popup_position();
});	


//Tiny Mice editor start 
        var clientstyle='css/editor.css';
        
        function initEditor(buttons,strwidth,strheight){	
       
         
            if (buttons == null){
              
                buttons ="bold,italic,underline,link,forecolor";
                
            }
            if($("#textarea_buttons").length>0)
               buttons = $("#textarea_buttons").val();
           
            if (strheight == null||strheight==""){
                strheight = "240px";
            }
            if (strwidth == null||strwidth==""){
                strwidth = "500px";
            }
          
            tinyMCE.init({
                theme : "advanced",
                mode : "textareas",
				editor_deselector : "NoEditor",
                plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
              
          
                setup : function(ed) {
                    
                
                    
                  ed.onPaste.add( function(ed, e, o) {
                	//	ed.execCommand('mcePasteText', true);
//                		return tinymce.dom.Event.cancel(e);
                	});
                    
                  ed.onKeyUp.add(function(ed, e) {
                       if($("#webcontent_text_count").length>0)
                       { 
                         if(!e.altKey && !e.shiftKey && !e.ctrlKey)
                            {
                                $("#webcontent_text_count").html($("#webcontent_text_count").attr("maxlength")-(fnCountTinyMice()));
                            }
                       }
                    });
                   ed.onKeyDown.add(function(ed,e) {
                       if($("#webcontent_text_count").length>0)
                       {
                           max=$("#webcontent_text_count").attr("maxlength");
                            
                           if(fnCountTinyMice()>=max)
                           {
                          
                            charvalueff=(e.which);
                            charvalueid=(e.keyCode);
                            if(!e.altKey && !e.shiftKey && !e.ctrlKey)
                            {
                                if((charvalueff>=48 && charvalueff<=57 )
                                ||(charvalueff>=65 && charvalueff<=90) 
                                ||(charvalueff>=96 && charvalueff<=111) 
                                ||(charvalueff>=186 && charvalueff<=222))  
                                {
                                    alert("Content reached maximum character length of " + max);
                                }
                                else if((charvalueid>=48 && charvalueid<=57 )
                                    ||(charvalueid>=65 && charvalueid<=90) 
                                    ||(charvalueid>=96 && charvalueid<=111) 
                                    ||(charvalueid>=186 && charvalueid<=222)  )
                                    {
                                        alert("Content reached maximum character length of " + max);
                                    }
                                else
                                {
                                    
                                }
                            }
                            
                                
                                
                                
                                
                           }
                       }
                    });
                                                          
                },
                paste_postprocess: function(pl,o)
                {
                  
                   if($("#webcontent_text_count").length>0)
                   {
                    $("#webcontent_text_count").html($("#webcontent_text_count").attr("maxlength")-(fnCountTinyMice()+o.content.length));
                   }
                       
                },
                paste_preprocess : function(pl, o) {
                     
                	var contH = o.content;
                    
                //	contH = contH.replace(/&nbsp;/g, '');
//                	contH = contH.replace(/<p ([^>]+)>|<p>/g, '<span>');
//                	contH = contH.replace(/<\/p>/g, '</span>');
//                	contH = contH.replace(/<span ([^>]+)>|<span>([^ ]+)<\/span>/g, '');
//                	contH = contH.replace(/<span><\/span>/g, '');
//                	contH = escape(contH);


                    //strip the html
                    contH = contH.replace(/<\/p>/g, '<\/p>\\n');
                    contH = contH.replace(/(<([^>]*)>)/ig,"");
                    contH = contH.replace(/\\n/g, '<\/br>');
                    contH = contH.replace(/&nbsp;/g,"");
                  
                   
                    contH = escape(contH);
                	for(var c in CharsTranslation){
                		var re = new RegExp(c, "g");
                		contH = String(contH).replace(re, CharsTranslation[c]);
                	}
                	contH = unescape(contH);
                    
                	o.content = contH;
                    if($("#webcontent_text_count").length>0)
                    {
                        max=$("#webcontent_text_count").attr("maxlength");
                       
                        if((fnCountTinyMice()+o.content.length)>=max)
                        {
                             alert("Content reached maximum character length of " + max);
                             o.content ="";
                        }
                    }
                    
                  
                },

             
                //theme_advanced_buttons_add : "styleselect",
                theme_advanced_toolbar_location : "top",
                theme_advanced_toolbar_align : "left",
                theme_advanced_buttons1 : buttons,//"bold,italic,underline,separator,"
            //    + "justifyleft,justifycenter,justifyright,bullist,separator,link,unlink,separator,styleselect,separator,cleanup",
                theme_advanced_buttons2 :"",    
                theme_advanced_buttons3 : "",
                content_css :clientstyle,
				//invalid_elements : "p",
                extended_valid_elements : "br,hr[class|width|size|noshade],font[color],span[style|class|align|id],div[id|class|align],p[id|class|align],strong,b,em,u",
                remove_script_host : false,
                relative_urls : false,
				remove_linebreaks : true,
                force_p_newlines:false,
                force_br_newlines:true,
				forced_root_block : '',
                height:strheight,
                width:strwidth
            });
            
           //  if($("#webcontent_text_count").length>0)
//               {
//                   if (typeof tinyMCE != "undefined") {
//        		   for(id in tinyMCE.editors){
//        			   tinyMCE.execCommand('mceFocus', false, id);
//        		   }
//        		   $('form:not(.filter) :input:visible:first').focus() ;
//        
//        		 }
//                
//                $("#webcontent_text_count").html($("#webcontent_text_count").attr("maxlength")-(fnCountTinyMice()));
//                
//               } 
             
          
        }
        
		
		if (typeof tinyMCE != "undefined") {
		  		   
			   for(id in tinyMCE.editors){
				   tinyMCE.execCommand('mceRemoveControl', false, id);
			   }
		}
	//	try{initEditor();} catch(e) {alert("Error:"+e)}
    
    
//Tiny Mice editor end


//Google Map Flash start 
    
    var myPano;
    
    function gm_initialize() {
      var fenwayPark = new GLatLng(-37.855584,145.021334);
      panoramaOptions = { latlng:fenwayPark };
      myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
      GEvent.addListener(myPano, "error", handleNoFlash);
    }
    
    function handleNoFlash(errorCode) {
      if (errorCode == FLASH_UNAVAILABLE) {
        alert("Error: Flash doesn't appear to be supported by your browser");
        return;
      }
    }  
    
//Google Map Flash end
  

function fnMenuOver(item)
{
    if($(item).attr("current")!="yes")
        $(item).css("color","#f7dc73");
}


function fnMenuOut(item)
{
    
    
    if($(item).attr("current")!="yes")
        $(item).css("color","#f1f2f2");
}

function fnMenuImg(item,img)
{
     if($(item).attr("active")!="yes")
        $(item).find("img").attr("src",img); 
}

function fnLoadStoreList(category_id)
{
$.ajax({
					
			url:'ajaxpageContent.php?page=Directory&category_id='+category_id,
		    success:function(htmls){
		      $('#content_Store').html(htmls);
                 fnResizeStoreList();
                  initCms();
                $.ajax({
									
        			url:'ajaxpageContent.php?page=StoreListCMS&category_id='+category_id,
        		    success:function(htmls){
        		     
        		      $('#storeAddCMS').html(htmls);
                       initCms();
                      
                      
                      }
        		   });
             
              }
		   });
     
}



function fnResizeStoreList()
{
   
    intCellMidHeight=$("#ContainerContent1").height();
    intStoreListHeight=$("#content_Store").height();
    intHeadingHeight=$("#ContainerContent1").find(".CellHeading").height();
    intHrHeight=$("#ContainerContent1").find(".CellHr").height();
    intTotalHeight=intStoreListHeight+intHeadingHeight+intHrHeight;

    if(intCellMidHeight<intTotalHeight)
    {
        $("#ContainerContent1").height(intTotalHeight+40);
        $("#ContainerContent1").find(".Cell").each(function(){
         
          $(this).height(intStoreListHeight+30);
         
        });
          $("#ContainerContent1").find(".CellMid").each(function(){
        
          $(this).height(intStoreListHeight+30);
         
        });
        
        
        
        
    }
    
   
    if(intTotalHeight<1060)
    {
        
        $("#ContainerContent1").height(1000+intHeadingHeight+intHrHeight+40);
          $("#ContainerContent1").find(".Cell").each(function(){
         
          $(this).height(1000+30);
         
        });
         $("#ContainerContent1").find(".CellMid").each(function(){
        
          $(this).height(1000+30);
         
        });
        
    }
  
        
}



function fnKeyDownSearch(event)
{
    
    if(event.which==13||event.keyCode==13)
        fnSearchStore();
    
}
function fnSearchStore()
{
   
    
    store_name=document.getElementById("txtSearch").value;
    if(store_name!="")
    {
           $.ajax({
					
			url:'ajaxpageContent.php?page=Search&store_name='+store_name,
		    success:function(htmls){
		      $('#content_Store').html(htmls);
                 fnResizeStoreList();
                  initCms();
              }
		   });
    }
    else
    {
        alert('Nothing to Search.');
    }
     
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


function fnHSASignUp()
{
    strname=document.getElementById("name").value;
    stremail=document.getElementById("email").value;
    
   
    if(strname=="")
    {
        alert("Please enter your name.");
        document.getElementById("name").focus();
        return;
    }
    
    else if(echeck(stremail) == false)
    {
        document.getElementById("email").focus()
        return;
    }
    
    else
    {
           $.ajax({
		    url:'ajaxpageContent.php?page=SignUp&name='+strname+'&email='+stremail,
		    success:function(htmls){$('#content_hsasignup').html(htmls);}
		   });
    }
   
   
}

function fnShowProcessing()
{
     
    $("#popupContainer__").html($("#popupHtml__").html());


	$('#close_button').hide();
	$('#submit_buttons').hide();
	$('#popup_content').text('');
	$('#popup_content').hide();
	$('#popup_submit').show();
	$('#popup_loading').show();
	$('#popup_title').html('Approving Store...'); // '+var1+' Banner');
   
        
	$('#popup_title').show();
    
	popup_position();
	//alert(page);
	$('#javascript_popup_mask').css('opacity',0);
	$('#javascript_popup_mask').show();
	$('#javascript_popup').show();
}

function fnShowApproveError(error)
{
   	$('#popup_loading').hide();	
    $('#popup_content').addClass('cms-text');
	$('#popup_content').css('color','red');
	$('#popup_content').html(error);
    $('#popup_content').show();
	$('#close_button').show();
    popup_position();
}

function fnShowApproveSuccess(success)
{
    $('#close_button').show();
    $('#popup_content').addClass('cms-text');
	$('#popup_content').css('color','green');
	$('#popup_content').html(success);
	$('#popup_loading').hide();
	$('#popup_content').show();
    
	popup_position();
}
function fnApproveStore(storerequest_id)
{
     fnShowProcessing();
    
    
      $.ajax({
                dataType: 'json',
    		    url:'ajaxpageContent.php?page=ApproveStore&storerequest_id='+storerequest_id,
    		    success:function(data)
                {
                    
    		      	   if(data.error)
                       {
                            fnShowApproveError(data.error);
    		      	   }    
    		           else if(data.success)
                       {
                          
                           	fnShowApproveSuccess(data.success)
                          	$.ajax({
    											
    									url:'ajaxpageContent.php?page=StoreRequest',
    								    success:function(htmls){
    								        $('#ContainerContent').html(htmls);
                                         }
    								});
                       }
            
                          
    		          
                },
            	error:function()
                {
				    alert('error');
				}
            });
}


function fnCountTinyMice()
{
    txt = tinyMCE.activeEditor.getContent({format : 'raw'});
    //txt = tinyMCE.editors[0].getContent({format : 'raw'});
    
    //strip the html
    txt = txt.replace(/(<([^>]*)>)/ig,"");
    txt = txt.replace(/&nbsp;/g,"");
    return txt.length;
    
}


 

function fnNewsCust()
{
    $("#newscontent").find("table").attr("bgcolor","");
    $("#fromElements").find("a").each(function(){
        
            $(this).click(function(){
                
                   	$.ajax({
											
									url:'ajaxpageContent.php?page=hsaba_news&newsid='+ $(this).attr("newsid"),
								    success:function(htmls){
								        $('#newscontent').html(htmls);
                                        $("#newscontent").find("table").attr("bgcolor","");
                                     }
								});
            });
         
    });    
}
