var nearbyMarkers = new Array();
var nearbyMarkersMore = new Array();
function doJSON (transport) {
        try { eval(transport.responseText); } catch (e) { }
} 
function showHideOklaTravel() {
    if ($('oklatravel_video').style.display == 'none') {
        $('oklatravel_video').style.display = 'block';
    } else {
        $('oklatravel_video').style.display = 'none';
    }
}
function toggleOklatravel() {
    if ($('oklatravel_video').style.display == 'block') {
        Effect.toggle('evergreen_video','slide', {beforeStart: showHideOklaTravel});
    } else {
        Effect.toggle('evergreen_video','slide', {afterFinish: showHideOklaTravel});
    }
    if ($('videoButton').src == 'http://static.newsok.biz/sites/wimgo/images/play_video.gif') {
        $('videoButton').src = 'http://static.newsok.biz/sites/wimgo/images/close_video.gif';
    } else {   
        $('videoButton').src = 'http://static.newsok.biz/sites/wimgo/images/play_video.gif';
    }
}
function showHideItems(i) {
    try {
        $(i+'_toggle').className=($(i+'_toggle').className == 'togglePlus')? "toggleMinus":"togglePlus";
        Effect.toggle(i+'_content','slide',{duration:0.5});
    } catch (e) { }
}
function toggleMapLarge(i, x, y, z, w) {
    new Effect.Morph(x,{style:'height:0', duration:0.5});
    new Effect.Morph(i,{style:'width:665px; height: 447px; right:308px', duration:0.5});
    Effect.Fade(y,{duration:0.5});
    Effect.Appear((z), {queue: 'end', duration:0.5});
    new Effect.Morph(w,{style:'height:408px', duration:0.5, afterFinish: function() {mapper.checkResize();}});
}
function toggleMapSmall(i, x, y, z, w) {
    new Effect.Morph(x,{style:'height:327px', duration:0.5});
    new Effect.Morph(i,{style:'width:298px; height: 315px; right:0px', duration:0.5});
    Effect.Fade(z, {duration:0.5});
    Effect.Appear((y), {queue: 'end', duration:0.5});
    new Effect.Morph(w,{style:'height:285px', duration:0.5, afterFinish: function(){mapper.checkResize();}});
}
function toggleMovieLarge(i, x, y, z, w) {
    new Effect.Morph(x,{style:'height:0', duration:0.5});
    new Effect.Morph(i,{style:'width:665px; height: 447px; right:308px', duration:0.5});
    Effect.Fade(y,{duration:0.5});
    Effect.Appear((z), {queue: 'end', duration:0.5});
    new Effect.Morph(w,{style:'height:408px', duration:0.5});
}
function toggleMovieSmall(i, x, y, z, w) {
    new Effect.Morph(x,{style:'height:309px', duration:0.5});
    new Effect.Morph(i,{style:'width:298px; height: 297px; right:0px', duration:0.5});
    Effect.Fade(z, {duration:0.5});
    Effect.Appear((y), {queue: 'end', duration:0.5});
    new Effect.Morph(w,{style:'height:255px', duration:0.5});
}
function fadeToggle(i) {
    try {
        Effect.toggle(i, 'appear',{duration:0.5});
    } catch (e) { }
}
function slideToggle(i) {
    try {
        Effect.toggle(i,'slide',{duration:0.5});
    } catch (e) { }
}
var footerState = 'closed';
function footerAppear(footer, content, content_text, content_header) {
    try {
        $('content_about').style.display = 'none';
        $('content_faq').style.display = 'none';
        $('content_whatis').style.display = 'none';
        $('content_opubco').style.display = 'none';
        $('header_about').style.display = 'none';
        $('header_faq').style.display = 'none';
        $('header_whatis').style.display = 'none';
        $('header_opubco').style.display = 'none';
        $(content_text).style.display = 'block';
        $(content_header).style.display = 'block';
        if (footerState == 'closed') {
            Effect.SlideDown(footer, {queue:'front', duration:0.5});
            Effect.Appear(content, {queue:'end', duration:0.5});
            footerState = 'opened';
        }
    } catch (e) { }
}
function footerClose() {
    Effect.SlideUp('footer_expand', {queue:'end', duration:0.5});
    Effect.Fade('footer_appear', {queue:'front', duration:0.5});
    footerState = 'closed';
}

function toggleText(i, x, y) {
    if (document.getElementById(i).innerHTML == x) {
        document.getElementById(i).innerHTML = y;
    } else {
        document.getElementById(i).innerHTML = x;
    }
}
function clearText(inputBox, clearText){
    if(inputBox.value == clearText){
        inputBox.value = '';
    }else{
        inputBox.select();
    }
}
function divSwap(element){
   var div = document.getElementById('tab_content');
   var nodeList = div.childNodes;
   var queue = Effect.Queues.get('menuScope');

   if(queue.toArray().length<1){
        if(Element.visible(element)==false){
            for(i=0;i<nodeList.length;i++){
                if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
                    if(Element.visible(nodeList.item(i))==true){
                        $(nodeList.item(i)).style.display="none";
                    }
                }
            }
            $(element).style.display="block";
        }
   }
}

function profileSwap(element){
   var div = document.getElementById('profile_content');
   var nodeList = div.childNodes;
   var queue = Effect.Queues.get('menuScope');

   if(queue.toArray().length<1){
        if(Element.visible(element)==false){
            for(i=0;i<nodeList.length;i++){
                if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
                    if(Element.visible(nodeList.item(i))==true){
                        Effect.Fade(nodeList.item(i),{queue:{position:'front',scope:'menuScope',limit:2},duration:0.5})
                    }
                }
            }
            Effect.Appear(element,{queue:{position:'end',scope:'menuScope',limit:2},duration:0.5})
       }
   }
    myProfileTab.className='notSelected';
    myAlertsTab.className='notSelected';
    myWidgetsTab.className='notSelected';
    myCoolTab.className='notSelected';
    $(element+'Tab').className=($(element+'Tab').className == 'selected')? "notSelected":"selected";
}

function getElementsByClassName(classname, node) {
      if(!node) node = document.getElementsByTagName("body")[0];
      var a = [];
      var re = new RegExp('\\b' + classname + '\\b');
      var els = node.getElementsByTagName("*");
      for(var i=0,j=els.length; i<j; i++)
      if(re.test(els[i].className))a.push(els[i]);
      return a;
}

function toggleNearby()
{
    for(i=0; i<nearbyMarkersMore.length; i++){
        if(Element.visible('viewMore_nearby')){
            mapper.removeOverlay(nearbyMarkersMore[i]);
        }else{
            mapper.addOverlay(nearbyMarkersMore[i]);
        }
    }
    slideToggle('viewMore_nearby');
    toggleText('viewMore_nearby_text', 'See more...', 'See less...');
}
function getNearby(venue_id, lat, lon, cat, page)
{
    page = page || 1;
   var url = "/ajax/movies/nearby.php?venue_id="+venue_id+"&lat="+lat+"&lon="+lon+"&cat="+cat+"&page="+page;
   new Ajax.Request(url, { method:'get', onSuccess: function(transport) { doJSON(transport); }});
}

function displayMoviesNearby(nearbyReturn)
{
    var j = 0;

    clearPoints();
    nearbyMarkers = new Array();
    nearbyMarkersMore = new Array();
    
    var nearby = nearbyReturn.nearby;
    var featured_nearby = nearbyReturn.featured_nearby;
    var venue_id = nearbyReturn.venue_id;
    var lat = nearbyReturn.lat;
    var lon = nearbyReturn.lon;
    var cat = nearbyReturn.cat_selected;
    var page = nearbyReturn.page;

    var count = nearbyReturn.count;
    var lim = nearbyReturn.limit;
    var pages = Math.floor(count/lim);

    var nextPage = ((Number(page)+1) <= pages)? (Number(page)+1):1;
    var prevPage = ((page-1) < 1)?pages:Number(page)-1;
  
    if(nearby.length >5) {
        j = 5;
    } else {
        j = nearby.length;
    }
    
    

    var divList = Builder.node('div',{id:'top_nearby_contain', className:'list_padding'});
    ulTop = Builder.node('ul',{});
    for(i=0; i<j; i++)
    {
        var li = Builder.node('li',{id:'nearby_'+nearby[i].id},[Builder.node('a',{href:'http://wimgo.com/venue/'+nearby[i].id},[Builder.node('div',{className:'nearby_sdistance_element'}, [nearby[i].distance+' miles']), Builder.node('div',{className:'nearby_title_element'}, [nearby[i].title])])]); 
        ulTop.appendChild(li);
        nearbyMarkers.push(addPoint(nearby[i], li,true ,'http://static.newsok.biz/sites/wimgo/images/mapicon_o_' + nearby[i].cat_id + '.png', true));
    }
    divList.appendChild(ulTop); 
    


    var divFeaturedList = Builder.node('div',{id:'top_featured_nearby_contain', className:'list_padding'});
    ulFeatured = Builder.node('ul',{});


    for(k=0; k<featured_nearby.length; k++)
    {
        var li = Builder.node('li',{id:'featured_nearby_'+featured_nearby[k].id}, [Builder.node('a',{href:'/venue/'+featured_nearby[k].featured_venue_id}, [Builder.node('div',{className:'nearby_distance_element'}, [featured_nearby[k].distance+' miles']), Builder.node('div',{className:'nearby_title_element'}, [featured_nearby[k].title])])]);
        ulFeatured.appendChild(li);
        nearbyMarkers.push(addPoint(featured_nearby[k], li,true ,'http://static.newsok.biz/sites/wimgo/images/star_icon.png', true));
    }
    divFeaturedList.appendChild(ulFeatured); 




    var hidden = true;
    if ($('viewMore_nearby')) {
        hidden = !Element.visible('viewMore_nearby');
    }

    if(nearbyReturn.page > 1){
        addToMap = true;
        navText = 'See less...';
     }else{
        addToMap = false;
        navText = 'See more...';
    }

    if(j >= 5){
        divListBottom = Builder.node('div',{id:'viewMore_nearby', style:hidden?'display:none;':''});
        ulBottom = Builder.node('ul',{});
       //» «
        var prev = 'getNearby('+ venue_id +',' +lat+',' +lon+',' +cat+',' +prevPage+')';
        var next = 'getNearby('+ venue_id +',' +lat+',' +lon+',' +cat+',' +nextPage+')';
        var count= ' ' + page + ' of ' + pages + ' '; 
        var nodePrev = Builder.node('a', {id:'nearbyPrev', href:'javascript:void(0);', onclick:prev}, 'Prev');
        var nodeCount = Builder.node('span', {}, [count])
        var nodeNext = Builder.node('a', {id:'nearbyNext', href:'javascript:void(0);', onclick:next}, 'Next');

        var ulPrevNext = Builder.node('ul', {'id':'nearbyPagination', 'class':'pages', 'style':'padding:10px 0 5px 0;'}, [
            Builder.node('li', {'class':'pageBtn'}, [nodePrev]),
            Builder.node('li', {'class':'pageCount'}, [nodeCount]),
            Builder.node('li', {'class':'pageBtn'}, [nodeNext])
        ]);
        
        for(i=j; i<nearby.length; i++)
        {
            var li = Builder.node('li',{id:'nearby_'+nearby[i].id},[Builder.node('a',{href:'/venue/'+nearby[i].id},[Builder.node('div',{className:'nearby_sdistance_element'}, [nearby[i].distance+' miles']), Builder.node('div',{className:'nearby_title_element'}, [nearby[i].title])])]);
            ulBottom.appendChild(li);
            nearbyMarkersMore.push(addPoint(nearby[i], li,addToMap,'http://static.newsok.biz/sites/wimgo/images/mapicon_o_' + nearby[i].cat_id + '.png', true));
        }
        
        ulBottom.appendChild(
            Builder.node('li', {'style':'border:0px none;'}, [
                ulPrevNext,
                ulBottom.appendChild(Builder.node("br",{'clear':'all'}))
            ])
        );
        divListBottom.appendChild(ulBottom);
        
        
        divList.appendChild(divListBottom);
        divListNearby = Builder.node('div',{id:'viewMore_nearby_link', className:'imgoing_more'});

        divListNearbyahref = Builder.node('a', {href: 'javascript:void(0);', onclick:'toggleNearby();'});
        divListNearbyahref.appendChild(Builder.node('img', {src:'http://static.newsok.biz/sites/wimgo/images/see_more.png', style:'margin-right: 5px;', border:'0', align:'absmiddle'}));
        divListNearbyahref.appendChild(Builder.node('span', {id:'viewMore_nearby_text'}, navText));
        divListNearby.appendChild(divListNearbyahref);
        divList.appendChild(divListNearby);

    }

    div = $('nearby_list_content');
    div.update('');
    div.appendChild(divFeaturedList);
    div.appendChild(divList);

 //   $(div).hide();
 //   Effect.Appear(div);
}

function givePref(profile_id, pref_id, module_id, module_type_id){
    if(!eProfile.isAuth()){
        alert('Please sign in to wimgo.');
        AuthManager.showLogin()
        return true;
    }
    if(!pref_id){
        alert('Error: Please provide a preference id');
        return false;
    }
    if(!module_id){
        alert('Error: Please provide a module id');
        return false;
    }
    if(!module_type_id){
        alert('Error: Please provide a module type id');
        return false;
    }

    var url = "/ajax/okla365/pref.php";
    params  = "profile_id="+escape(profile_id)+"&pref_id="+escape(pref_id)+"&module_id="+escape(module_id)+"&module_type_id="+escape(module_type_id);
    new Ajax.Request(url, { method:'get', parameters: params, onSuccess: function(transport) { doJSON(transport); }});
}
function prefGiven(data){
    if(data.success == 1){
        for(i=0; i<data.prefs.length; i++){
            $('pref_count_'+data.prefs[i].pref_id).innerHTML = data.prefs[i].pref_count;
            $('pref_'+data.prefs[i].pref_id).addClassName('imgoing_button');
            $('pref_'+data.prefs[i].pref_id).removeClassName('imgoing_button_selected');
        }
        $('pref_'+data.pref).addClassName('imgoing_button_selected');
    }else{
        $('pref_msg').innerHTML = "A problem was encoutered.";
    }
}
function getPref(module_id, module_type_id){
    if(!eProfile.isAuth()){
        return true;
    }
    if(!module_id){
        alert('Error: Please provide a module id');
        return false;
    }
    if(!module_type_id){
        alert('Error: Please provide a module type id');
        return false;
    }

    var url = "/ajax/okla365/pref.php";
    params  = "option=get_pref&module_id="+escape(module_id)+"&module_type_id="+escape(module_type_id);
    new Ajax.Request(url, { method:'get', parameters: params, onSuccess: function(transport) { doJSON(transport); }});
}
function prefGotten(data){
    if(data.success == 1){
        $('pref_'+data.pref).addClassName('imgoing_button_selected');
    }
}
function deleteItem(module_id, module_type_id){
/*
    if(!eProfile.isAuth()){
        alert('Sorry, you do not have permission to delete this item.');
        return true;
    }
*/
    if(!module_id){
        alert('Error: Please provide a module id');
        return false;
    }
    if(!module_type_id){
        alert('Error: Please provide a module type id');
        return false;
    }

    var okay = confirm("Are you sure you want to delete this item?\n\nWARNING: IF YOU DELETE THIS ITEM IT WILL BE PERMANENT, THERE IS NO GOING BACK!");
    if(okay){
        var url = "/ajax/okla365/delete.php";
        params  = "module_id="+escape(module_id)+"&module_type_id="+escape(module_type_id);
        new Ajax.Request(url, { method:'get', parameters: params, onSuccess: function(transport) { doJSON(transport); }});
    }
}
function itemDeleted(data){
    if(data.success == 1){
        window.location.href="/";
    }else{
        alert(data.msg);
    }

}
function invite_friend(tf_url){
    var email = $('tf_email').value;
    var message = $('tf_message').value;
    var url = "/ajax/okla365/invite_friend.php?email="+email+"&message="+message+"&url="+tf_url;
    new Ajax.Request(url, { method:'get', onSuccess: function(transport) { doJSON(transport); }});
}
function invited_friends(data){
    if(data.success == 1){
        $('tf_msg').innerHTML = "Email sent.";
       new Effect.Highlight('tf_msg', {queue:'front',startcolor:'#ff99ff', endcolor:'#999999'})
       Effect.Fade('tf_msg',{queue:'end',duration:'.7'});
    }else{
        $('tf_msg').innerHTML = "A problem was encoutered sending email.";
    }
}
function getDirections(start,end){
    new Ajax.Request('/ajax/okla365/directions.php?start='+escape(start)+"&end="+escape(end));
   url = 'http://maps.google.com/maps?f=d&hl=en&geocode=&time=&date=&ttype=&saddr='+escape(start)+'&daddr='+escape(end)+'&sspn=0.276491,0.752563&ie=UTF8&z=11&om=1';
    window.open(url,'directions','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
}
function toggleMoreEvents(){
    slideToggle('viewMore_upcoming');
    toggleText('viewMore_upcoming_text', 'See more...', 'See less...');
}
function print_friendly(event, item){
    console.log(item.title);
}
function changeRelatedTab(tabID) {
    document.getElementById('comments_tab').className = "notselected";
    document.getElementById('videos_tab').className = "notselected";
    document.getElementById('photos_tab').className = "notselected";
    document.getElementById('news_tab').className = "notselected";
    document.getElementById(tabID).className = "selected";
    if(tabID != 'videos_tab') unLoadVideoBar();
}
var searchText = 'Type your search here.';
function clearSearchText(srchBox) {
    if (srchBox.value == searchText) {
        srchBox.value = "";
    } else {
        srchBox.select();
    }
}
/**
 * Manages the Notify Us form on the details page
 */
AmissForm = {
    initialized:false,
    
    initialize:function()
    {
        if(this.initialized) return;
        this.initialized = true;
        
        this.type_id = $('module_type_id').value;
        this.module_id = $('module_id').value;
        this.form = $("amissForm");
        this.reason = $('amiss_text');
        this.msg = $('amissMsg');
    },
    
    /**
    *   Shows the form in the specified node.
    *   @param HTMLElement  anchor  The anchor tag that was clicked to open the form. 
    *   @param string target    The id of the node that will contain the report form.
    *   @param int    typeid    The module id of the comment.  (FYI Comments are "3")
    *   @param string cid       The  id of the comment being reported.
    */
    show:function()
    {    
        this.initialize();
        new Effect.toggle(this.form, 'slide', {duration:1, queue:'end'});

    },
    
    /**
    *   resets the form fields
    */
    reset:function()
    {
        //clear the form to defaults.
        this.reason.value = '';
    },

    /**
    *   Attempts to send the report.
    *   Expects to find hidden form fields with the module's id and module's type.
    */
    submit:function()
    {
        if(this.reason.value.length == 0) return;
        
        var msg = "We appreciate you letting us know about your concern.  We will review your report and take the appropriate action.  Please be aware that circumstances may prevent us from taking immediate action.";
        this.msg.innerHTML = msg;
        
        //code to send report
        var url = '/ajax/requestJSON.php?';
        url+="method=Objection.AddObjection";
        
        url+="&module_type_id="+this.type_id;
        url+="&module_id="+this.module_id;
        url+="&reason="+escape(escape(this.reason.value));

        this.loadJSON(url);
        this.reset();
        this.show();
    },

    /**
    *  Creates a new script node in the HEAD pointing to a php file that will process the url's 
    *  query string params and return a JSON result.
    *
    *  @param string url A url to the php page that will produce the required javascript code.
    */
    loadJSON:function(url)
    {
       try{
            var headID = document.getElementsByTagName("head")[0];         
            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.src = url;
            headID.appendChild(newScript);
         } catch(e){
            alert("error " + e.message);
         }
    }
}
