function paginare_ajax(){
	window.ajaxPagination = new AjaxPagination(this.arg1,this.arg2, this.arg3, null, this.arg4, this.arg5, this.arg6);
        window.ajaxPagination.buttons = {firstPage: '<<', prevSection: false, prevPage: '<', nextPage: '>', nextSection: false, lastPage: '>>'};
        window.ajaxPagination.setShowButtonsIfInactive(true);
        window.ajaxPagination.setCallbackFunction('fetchComments');
	    window.ajaxPagination.paginate();
};

function deleteChildNodes(parent) {
    while (parent.childNodes[0]) {
        parent.removeChild(parent.childNodes[0]);
    }
}

// Favorite
function add_favorite(content_id) {
    if (window.elementCache.exists('favorite')) {
        deleteChildNodes($('video_options_container'));
        window.elementCache.deploy('favorite', $('video_options_container'));
        return true;
    }

	customParams = {content_id: content_id};
	sn_getAjax('add_favorite', customParams);
}

function sn_addFavoriteHandler(result, resultElementName, userData)
{
	var code = result.code;
	result = result.text.evalJSON();
	try {
		$('video_options_container').update(result.text);
		window.elementCache.save('favorite', $('video_options_container'));
	} catch(e) {
		$('add_favorites').update(result.text);
		if (code == 0) {
		    $('delete_favorites').update('<br/><input type="submit" onclick="javascript:delete_favorite(\'' + result.id + '\');" value="Scoate din favorite">');
		}
	}
}

function delete_favorite(content_id) {
	customParams = {content_id: content_id};
	sn_getAjax('delete_favorite', customParams);
}

function sn_deleteFavoriteHandler(result, resultElementName, userData) {
	$('video_options_container').update(result.text);
    window.elementCache.deleteItem('favorite');
}


// Adaugare grup
function loadGroupsToggle(contentId) {
    if (window.elementCache.exists('groups')) {
        window.elementCache.deploy('groups', $('video_options_container'));
        return true;
    }
	customParams = {id: contentId,save_from: 'content' };
	sn_getAjax('f_savetogroup', customParams);
};

function sn_fSavetogroupHandler (result, resultElementName, userData){
	var code = result.code;
	result = result.text.evalJSON();
	$('video_options_container').update(result.text);
	window.elementCache.save('groups', $('video_options_container'));
}


function s_loadPlaylistToggle(contentId) {
	var playlistsLoaded = ($('add_to_playlist_form_'+contentId) != null);
	if (!playlistsLoaded) {
		sn_updateByAjax('f_s_savelist',
			{
				save_from: 'content',
				id: contentId
			},
			'det_optiune_4'
		);
	} else emptyVideoContainer();
}

function s_loadGroupsToggle(contentId) {
    var groupsLoaded = ($('add_to_group_form_'+contentId) != null);
    if (!groupsLoaded) {
        groupsLoaded = true;
        sn_updateByAjax('f_s_savetogroup',
        	{
            	save_from: 'content',
            	id: contentId
        	},
            ($('video_options_container'))?'video_options_container':'det_optiune_6'
        )
    }
    else emptyVideoContainer();
};

// Adaugare playlist
function loadPlaylistToggle(contentId) {
    if (window.elementCache.exists('playlists')) {
        window.elementCache.deploy('playlists', $('video_options_container'));
        return true;
    }
	customParams = {id: contentId,save_from: 'content' };
	sn_getAjax('f_savelist', customParams, 'video_options_container');
}

function sn_fSavelistHandler (result, resultElementName, userData){
	var code = result.code;
	result = result.text.evalJSON();
	$(resultElementName).update(result.text);

	if (code == 222) {
	   window.elementCache.save('playlists', $(resultElementName));
	} else if (code == 223) {
	    window.elementCache.save('playlistsq', $(resultElementName));
	}
}


// Raporteaza
function loadReportForm(content_aid,is_logged) {
	if (window.elementCache.exists('report_form')) {
		window.elementCache.deploy('report_form', $('video_options_container'));
		return true;
	}
	params = {content_aid: content_aid}
	sn_getAjax('load_report_form',params,'video_options_container');
}

function sn_loadReportFormHandler(result, resultElementName) {
    $(resultElementName).update(result.text);
    window.elementCache.save('report_form', $(resultElementName));
}


// Embed
function loadEmbedToggle(contentId) {
	sn_updateByAjax('embedLinks', {id: contentId}, 'det_optiune_7');
}

function embedToogle (contentId,from){
	if (typeof window.GeoRestrictedContent != 'undefined') {
		$('video_options_container').update('<div class="option_container">Acest material nu poate fi preluat</div>');
		return;
	}
    if (window.elementCache.exists('embed')) {
        window.elementCache.deploy('embed', $('video_options_container'));
        return true;
    }

	customParams = {id: contentId, from: from };
	sn_getAjax('embed', customParams);
}

function sn_embedHandler (result, resultElementName, userData) {
	result = result.text.evalJSON();
	$('video_options_container').update(result.text);
	make_embed_code();
	window.elementCache.save('embed', $('video_options_container'));
}

function toggle_embed(ev,cid) {
	var elem = Event.element(ev), optionsContainer = $('video_options_container');
	if(elem.hasClassName('add')) {
		if (!window.elementCache.exists('embeded')) {
			use_embed_settings(cid);
			elem.removeClassName('add');
			elem.addClassName('add_minus');
		} else {
			$('embeded').style.display = 'block';
			elem.removeClassName('add');
			elem.addClassName('add_minus');
			window.elementCache.deploy('embeded', $('embeded'));
		}
	} else {
		//$('embeded').style.display = 'none';
		remove_embed();
		elem.removeClassName('add_minus');
		elem.addClassName('add');
	}
}

function use_embed_settings(cid) {
	if($A($$('#embeded .titlu_upl')).size() > 0) {
		Effect.Appear('embeded',{duration: 0.4});
	} else if(cid) {
		params = {content_id: cid};
		sn_updateByAjax('get_embed_settings', params,'embeded', {onComplete: embed_code_options});
	}
}

function embed_code_options() {
    window.elementCache.save('embeded', $('embeded'));
	if ($('form_embed')) {
		$('form_embed').getElements().each(function(el){
			if (el.type == 'radio') Event.observe(el, 'click', make_embed_code);
			else Event.observe(el, 'change', make_embed_code);
		});
	}
	$$('.color_picker_close').each(function(dv) {
		Event.observe(dv,'click',function(event) {
			var id = (/[^0-9]*([0-9]+)/.exec(dv.id))[1];
			toggleColorPicker(event, id, 'hide');
		});
	});
	$$('.color_picker_show').each(function(dv) {
		Event.observe(dv,'click', function(event) {
			var id = (/[^0-9]*([0-9]+)/.exec(dv.id))[1];
			toggleColorPicker(event, id, 'show');
		});
	});
	Effect.Appear('embeded',{duration: 0.4});
}

function remove_embed(){
	Effect.Fade('embeded',{duration: 0.4});
}






// itemNames
function deactivate(link) {
	link.removeClassName('hovered_' + link.id);
	link.removeClassName('activat');
	try {
		$('video_options_container').hide();
	}
	catch(e) {
		div = $('det_' + link.id);
		div.hide();
	}
	document.getElementById('loginux').style.display = 'none';
}

function emptyVideoContainer() {
	try {
		if( !$('video_options_container').empty() ) {
			$('video_options_container').update('');
		}
	}
	catch(e) {;}
}


function activateButton(link) {
	$$('a.activat').each(function(link) {deactivate(link);});
	div = $('video_options_container');
	link.addClassName('hovered_' + link.id);
	link.addClassName('activat');
	document.getElementById('loginux').style.display = 'none';
	try {
	    eval(link.onActivate);
		div.show();
	}
	catch(e) {
		div = $('det_' + link.id);div.show();
	}
}

function toggleOption(link) {
	var optionsContainer = $('video_options_container'), itemName = false;

	if (link.hasClassName('hovered_' + link.id)) {
		deactivate(link);
	} else {
		activateButton(link);
	}
}

function closeVideoOptionsContainer() {
	if ($('video_options_container') && $('video_options_container').visible()) {
	   $('video_options_container').hide();
	   $$('a.activat').each(function(link){deactivate(link);});
	   emptyVideoContainer();
	}
}

ElementCache = Class.create();
Object.extend(ElementCache.prototype, {
    stash: {},
    deleteItem: function (itemName) {
    	if (this.stash[itemName]) {
    		delete this.stash[itemName];
    	}
    },
    exists: function(itemName) {
        if (this.stash[itemName]) {
            return true;
        }
        return false;
    },
    deploy: function(itemName, parentContainer) {
    	var virtualContainer, childNodes, child, num, i;

    	if (this.stash[itemName]) {
    	    virtualContainer = this.stash[itemName];
    	    deleteChildNodes(parentContainer);
    		childNodes = virtualContainer.childNodes;
    		virtualContainer = this.stash[itemName];
    	    for (i = 0, num = childNodes.length; i < num; i++) {
    	        parentContainer.appendChild(childNodes[i].cloneNode(true));
    	    }
    		return true;
    	} else {
    		return false;
    	}
    },
    save: function(itemName, parentContainer) {
    	var virtualContainer, childNodes, child, i, num;
    	childNodes = parentContainer.childNodes;
    	virtualContainer = parentContainer.cloneNode(true);
    	this.deleteItem(itemName);
    	this.stash[itemName] = virtualContainer;
    }
});

window.elementCache = new ElementCache();

document.observe('dom:loaded', function() {
	$$('a.toggler').each(function(element) {
		Event.observe(element, 'click', function(event) {
			element = Event.findElement(event, 'a');
			toggleOption(element);
		});
	});

	if (typeof(window.GeoRestricted) != 'undefined') {
	    if ($('geoalert')) {
            $('geoalert').innerHTML = '<div class="geo_forbidden">Acest material nu poate fi vazut in tara dumneavoastra deoarece se afla sub incidenta copyright-ului.<br><br>This video is not available in your country due to copyright restrictions.</div>';
	    }
	}
});

function loadPlaylistToggleQ() {
    if (window.elementCache.exists('playlistsq')) {
        window.elementCache.deploy('playlistsq', $('playlist_form'));
    } else {
        sn_getAjax('f_savelist',
        {
            save_from: 'ql',
            id: 'ql'
        },
        'playlist_form'
        );
    }

    if($('playlist_form').visible()) {
        $('btn_toggle_s_ql').update('salveaza ca playlist');
        $('playlist_form').hide();
    }
    else {
        $('btn_toggle_s_ql').update('inchide lista');
        $('playlist_form').show();
    }
}

function load_embed_links() {

    if (window.elementCache.exists('embedContentLinks')) {
        window.elementCache.deploy('embedContentLinks', $('video_options_container'));
        return true;
    }
    sn_getAjax('embed_links', {id: contentId});
}

function sn_embedLinksHandler(result, resultElementName, userData)
{
	var code = result.code;
	try {
		$('video_options_container').update(result.text);
		window.elementCache.save('embedContentLinks', $('video_options_container'));
	} catch(e) {
		$('video_options_container').update(result.text);
	}
}


function setBlackLightLayerHeight() {
	if ($('divSchimbaLumina')) $('divSchimbaLumina').setStyle({'height': getPageDimensions().height});
}

function schimbaLumina() {
	setBlackLightLayerHeight();
	var mode = $$('BODY')[0].hasClassName('luminaStinsa') ? 'off' : 'on';
	if (mode == 'on') {
		if (!$('divSchimbaLumina')) {
			new_div = new Element('DIV', {'id': 'divSchimbaLumina', 'style': 'height: ' + getPageDimensions().height + 'px;'});
			document.body.appendChild(new_div);
			Event.observe($('divSchimbaLumina'), 'click', schimbaLumina);
		}
		else {
			$('divSchimbaLumina').setStyle({'display': 'block'});
		}
		$$('BODY')[0].addClassName('luminaStinsa');
		$$('.cinemaBoard A')[0].removeClassName('light_on').addClassName('light_off');
	}
	else {
		$('divSchimbaLumina').setStyle({'display': 'none'});
		$$('BODY')[0].removeClassName('luminaStinsa');
		$$('.cinemaBoard A')[0].removeClassName('light_off').addClassName('light_on');
	}
}

function playerHC() {
	var narrowPlD = {0:'640px', 1:'390px'};
	var widePlD = {0:'800px', 1:'480px'};
	var widePlD_back = {0:'960px', 1:'480px'};
	var curPlWidth = $$('.player_bg')[0].getStyle('width');
	var nextD = (curPlWidth == narrowPlD[0]) ? widePlD : narrowPlD;
	var nextD_back = (curPlWidth == narrowPlD[0]) ? widePlD_back : narrowPlD;
 	$$('.player_bg')[0].setStyle({'width': nextD[0]});
 	$$('.player_bg')[0].setStyle({'height': nextD[1]});
 	if (curPlWidth == narrowPlD[0]) {
 		$$('.cinemaBoard A')[1].removeClassName('wide_off').addClassName('wide_on');
 	}
 	else {
 		$$('.cinemaBoard A')[1].removeClassName('wide_on').addClassName('wide_off');
 	}
 	$$('#content_det_show')[0].setStyle({'width': nextD_back[0]});
 	setBlackLightLayerHeight();
 	window.plW = $$('#player220' + ((Prototype.Browser.Gecko) ? ' OBJECT' : ''))[0];
 	setTimeout('window.plW.fit()', 30);
}
